Log inUsernamePassword
Log me on automatically each visit    
Register
Register
Log in to check your private messages
Log in to check your private messages
Visual Basic Forum for Visual Basic Programmers VB Forum Index » API

Post new topic   Reply to topic
Problem to sendkeys with focus
View previous topic :: View next topic  
Author Message
Json
Newbie


Joined: 13 Aug 2005
Posts: 14

PostPosted: Mar 6th, 2006 06:57 AM    Post subject: Problem to sendkeys with focus Reply with quote

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
View user's profile Send private message
P.T.A.M.
Administrator


Joined: 08 Oct 2003
Posts: 752
Location: Greece

PostPosted: Mar 6th, 2006 08:15 AM    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website ICQ Number
Json
Newbie


Joined: 13 Aug 2005
Posts: 14

PostPosted: Mar 6th, 2006 09:11 AM    Post subject: Reply with quote

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
View user's profile Send private message
Json
Newbie


Joined: 13 Aug 2005
Posts: 14

PostPosted: Mar 7th, 2006 11:12 AM    Post subject: Reply with quote

can you show me some better ways?
Back to top
View user's profile Send private message
P.T.A.M.
Administrator


Joined: 08 Oct 2003
Posts: 752
Location: Greece

PostPosted: Jun 6th, 2006 06:30 AM    Post subject: Reply with quote

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 Wink
_________________
No one is completely useless. They can at least be an example of what to avoid.
Back to top
View user's profile Send private message Send e-mail Visit poster's website ICQ Number
Display posts from previous:   
Post new topic   Reply to topic    Visual Basic Forum for Visual Basic Programmers VB Forum Index » API All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Visual Basic Forum runs phpBB | Forum Template © iOptional
VB Resources | SSL | Visual Basic