| View previous topic :: View next topic |
| Author |
Message |
Json Newbie
Joined: 13 Aug 2005 Posts: 14
|
Posted: Mar 6th, 2006 06:57 AM Post subject: Problem to sendkeys with focus |
|
|
I am making a program to sendkey to another app.(word,excel,..whatever in the foreground ), if I use the function "sendkeys", I must make the background app on focus, but my program is like a screen keyboard, I don't want to make my app loss focus. here is my program, how should I make change to it ? THX
Option Explicit
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Dim PrevhWnd As Long
Private Sub Command1_Click()
'Lock the window update
'LockWindowUpdate GetDesktopWindow
PrevhWnd = GetForegroundWindow() 'Find out what window has focus
SetForegroundWindow PrevhWnd
'Set the parent
' SetParent PrevhWnd, Me.hwnd
'Put the focus on notepad
ShowWindow PrevhWnd, 6
' Putfocus PrevhWnd
Sleep (0)
DoEvents
SendKeys "ABC", 0
LockWindowUpdate False
End Sub
Last edited by Json on Mar 6th, 2006 10:06 AM; edited 1 time in total |
|
| Back to top |
|
P.T.A.M. Administrator

Joined: 08 Oct 2003 Posts: 752 Location: Greece
|
Posted: Mar 6th, 2006 08:15 AM Post subject: |
|
|
I tried your code with Notepad and it worked. What is the problem you are having? There are ways other than sendkeys to make this but they are quite a bit harder... _________________ No one is completely useless. They can at least be an example of what to avoid. |
|
| Back to top |
|
Json Newbie
Joined: 13 Aug 2005 Posts: 14
|
Posted: Mar 6th, 2006 09:11 AM Post subject: |
|
|
| I want my program always on top of the others, and have focus back after send out a key, but this program will make my program min and lost focus after sendkey |
|
| Back to top |
|
Json Newbie
Joined: 13 Aug 2005 Posts: 14
|
Posted: Mar 7th, 2006 11:12 AM Post subject: |
|
|
| can you show me some better ways? |
|
| Back to top |
|
P.T.A.M. Administrator

Joined: 08 Oct 2003 Posts: 752 Location: Greece
|
Posted: Jun 6th, 2006 06:30 AM Post subject: |
|
|
To change the focus of the window look for the SetForegroundWindow or the SetActiveWindow APIs.
To get the handle for the above APIs use the FindWindow API.
To find the handle of the currently active window you can use the GetForegroundWindow or the GetActiveWindow APIs...
Google is your friend  _________________ No one is completely useless. They can at least be an example of what to avoid. |
|
| Back to top |
|
|