Results 1 to 10 of 10

Thread: Keeping focus

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Location
    London Occupation: Desktop Developer
    Posts
    141

    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.

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    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.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    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.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  4. #4
    Registered User Olly's Avatar
    Join Date
    Apr 2001
    Location
    Switzerland
    Posts
    252
    I got the impression that these focus questions are making their round today

  5. #5
    Matthew Gates
    Guest
    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?

  6. #6
    Tygur
    Guest
    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

  7. #7
    Registered User Olly's Avatar
    Join Date
    Apr 2001
    Location
    Switzerland
    Posts
    252

    Thumbs up

    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.

  8. #8
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    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
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  9. #9
    Matthew Gates
    Guest
    ..more Visual Basic/API knowledge, blackjack tips, poker tips, and the charm that I have that gets girls to like/love me .

  10. #10
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    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
  •  



Click Here to Expand Forum to Full Width