|
-
Apr 30th, 2001, 11:04 AM
#1
Thread Starter
Addicted Member
Keeping focus
I am using sendkeys and mouse clicks to control an application, however I need the application to keep focus at all times, ie there are scheduled jobs that could kick off mid way and the app would loose focus and send keys would thengo pear shaped!
I have been struggling with API and wonder if anyone has some pointers.
-
Apr 30th, 2001, 01:25 PM
#2
Frenzied Member
I can think of putting a timer which will set the focus to the app using . (sure there is a better solution).
Code:
SetFocus
The SetFocus function sets the keyboard focus to the specified window. The window must be associated with the calling thread's message queue.
HWND SetFocus(
HWND hWnd // handle to window to receive focus
);
Parameters
hWnd
Handle to the window that will receive the keyboard input. If this parameter is NULL, keystrokes are ignored.
Return Values
If the function succeeds, the return value is the handle to the window that previously had the keyboard focus. If the hWnd parameter is invalid or the window is not associated with the calling thread's message queue, the return value is NULL. To get extended error information, callGetLastError.
Remarks
The SetFocus function sends a WM_KILLFOCUS message to the window that loses the keyboard focus and a WM_SETFOCUS message to the window that receives the keyboard focus. It also activates either the window that receives the focus or the parent of the window that receives the focus.
If a window is active but does not have the focus, any key pressed will produce the WM_SYSCHAR, WM_SYSKEYDOWN, or WM_SYSKEYUP message. If the VK_MENU key is also pressed, the lParam parameter of the message will have bit 30 set. Otherwise, the messages produced do not have this bit set.
By using the AttachThreadInput function, a thread can attach its input processing to another thread. This allows a thread to call SetFocus to set the keyboard focus to a window associated with another thread's message queue.
-
Apr 30th, 2001, 01:26 PM
#3
Frenzied Member
Argh! Not C++.
Code:
Declare Function SetFocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
· hWnd
Identifies the window that will receive the keyboard input. If this parameter is NULL, keystrokes are ignored.
-
Apr 30th, 2001, 02:06 PM
#4
Registered User
-
Apr 30th, 2001, 02:14 PM
#5
Originally posted by Vlatko
Argh! Not C++.
Code:
Declare Function SetFocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
· hWnd
Identifies the window that will receive the keyboard input. If this parameter is NULL, keystrokes are ignored.
This will not work with VB, you will get an error since there is already a method called SetFocus, so you have to change it.
Code:
Private Declare Function PutFocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
Hey Vlatko? Can I make a copy of your brain? The C++ section?
-
Apr 30th, 2001, 02:44 PM
#6
SetFocus (or PutFocus, as Matt renamed it) can only be used to set the focus to windows within your program. You can't use it to set focus to another program. This makes it useless for you.
There is another API function, SetForegroundWindow, but this is probably also useless, because later versions of windows don't let you use it to bring a window to the foreground unless your program is in the foreground. There is a workaround, though. You can find it if you go to this site and look for ForceFore.zip:
http://www.mvps.org/vb/index2.html?samples.htm
Here's the declaration for SetForegroundWindow in case you might want it:
Private Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Long) As Long
-
Apr 30th, 2001, 03:00 PM
#7
Registered User
Have a look into the Focus thread. I think you can use the
SetTimer in intervalls about 200 ms, then you check the getfocus, if it's 0 then you use the setfocus. That's it.
-
Apr 30th, 2001, 04:57 PM
#8
Frenzied Member
Originally posted by Matthew Gates
Hey Vlatko? Can I make a copy of your brain? The C++ section?
There are other nice parts of my brain besides the C++ one .
Well let's change brain parts, what do i get. LOL
-
Apr 30th, 2001, 06:46 PM
#9
..more Visual Basic/API knowledge, blackjack tips, poker tips, and the charm that I have that gets girls to like/love me .
-
May 1st, 2001, 03:16 AM
#10
Frenzied Member
What works in NT4 (I haven't tested it on other platforms) is to subclass your application's main form and whenever you recieve a WM_ACTIVATEAPP message (&H1C) then return false if wParam is false.
The parameters for this message are:
wParam - TRUE if the application recieving the message is the one being activated
lParam- The ThreadId of whatever application is being activated.
That said, would it not be an idea to substitute the SendMessage API function and send WM_CHAR (&H102) messages to the target window? This will be unaffected by the user monkeying around with the focus.
The parameters for this message are:
wParam- Virtual key number (e.g. vbKeyA)
lParamBits (0-15) Repetitions
Bits (16-23) - Scan Code
Bit (24) - 1 if its an extended key
Bit (29) - 1 if alt was down
Bit (30) - 1 if this is an autorepeat key
Bit (31) - 1 if key up, 0 if key down
HTH,
Duncan
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|