Results 1 to 14 of 14

Thread: Keypress outside form

Hybrid View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2007
    Posts
    133

    Keypress outside form

    I am trying to figure out how to code my program to notice when the "E" key is pressed outside of the form. How do i do this I am kind of lost. I have tried the following so far.....
    What value do i put in for "e" i read its a numeric value and i found the virtual key list in the MSDN but the value it give me (in hex) doesnt work.
    Is the getasynckeystate the right api to use? once this works i want to check for a couple more keys as well.

    Code:
    Private Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vkey As Integer) As Short
        Dim test As Short
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            test = GetAsyncKeyState("e")
    
    
        End Sub
    VB version: Visual Studio 2008-Professional

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Oct 2007
    Posts
    133

    Re: Keypress outside form

    or is there a way to "listen" to the keyboard and trigger an even when a key is pressed? even if my program/form isnt the actively selected?
    VB version: Visual Studio 2008-Professional

  3. #3
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Keypress outside form

    Take a look at the RegisterHotKey API.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2007
    Posts
    133

    Re: Keypress outside form

    Quote Originally Posted by iPrank
    Take a look at the RegisterHotKey API.
    Thanks but i dont think i understand it fully. I have tried the following and im not sure what to use as the id and virtual key code. Ive tried looking up the keycode for "E" and i cant find anyhthing that it lets me enter as vk in the function.

    Code:
    Public Class Form1
        Declare Function RegisterHotKey Lib "user32" Alias "RegisterHotKey" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
        Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            'get window handle
            Dim strCaption As String, lhWnd As Long
            strCaption = "Form1"
            lhWnd = FindWindow(vbNullString, strCaption)
    
            If lhWnd = 0 Then
                MsgBox("Could not find Form1...")
            Else
                MsgBox("Form1 found: " & lhWnd)
            End If
    
    
            'key check
            Dim keypress As Long, ek As System.Windows.Forms.KeyEventArgs
            keypress = RegisterHotKey(lhWnd, , , "NEED THIS") 'dont understand the arguments here, i can get the form handl though.
    
    
    
        End Sub
    End Class
    VB version: Visual Studio 2008-Professional

  5. #5
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Keypress outside form

    Hai stogie,
    Code:
    ret = RegisterHotKey(Me.hwnd, &HBFFF&, 0&, vbKeyE)

    The 3rd parameter, you can specify wheather you need to associate another key with E to make a combination. i.e. CTRL + E

    Parameters are
    MOD_ALT
    Either ALT key must be held down.
    MOD_CONTROL
    Either CTRL key must be held down.
    MOD_SHIFT
    Either SHIFT key must be held down.
    MOD_WIN

    so if you want the ctrl to be pressed with E, then this paramerer would be MOD_CONTROL
    Last edited by Fazi; Jul 21st, 2008 at 01:09 PM.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2007
    Posts
    133

    Re: Keypress outside form

    Fazi, Thanks.
    i take it 0& means there is no associate key (i dont want an associate key).
    also do i have to specify the vbKeyE part somewhere? VS2008 isnt recognizing the term "vbKeyE".
    VB version: Visual Studio 2008-Professional

  7. #7
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Keypress outside form

    Quote Originally Posted by Stogie03
    Fazi, Thanks.
    i take it 0& means there is no associate key (i dont want an associate key).
    also do i have to specify the vbKeyE part somewhere? VS2008 isnt recognizing the term "vbKeyE".
    oh i am vb6

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Oct 2007
    Posts
    133

    Re: Keypress outside form

    Nevermind i found that 69 is the code for E.

    How on my form do i check or keep checking if E has been pressed now? As of right now it is in the form load event, but its not checking over and over.
    VB version: Visual Studio 2008-Professional

  9. #9
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Keypress outside form

    Quote Originally Posted by Stogie03
    Nevermind i found that 69 is the code for E.

    How on my form do i check or keep checking if E has been pressed now? As of right now it is in the form load event, but its not checking over and over.
    the registration is system wide.
    you can unregistered it when you dont need.

    did you see that example found in the link iprank gave? play with that first little


  10. #10
    Fanatic Member
    Join Date
    Jan 2007
    Location
    Middletown, CT
    Posts
    948

    Re: Keypress outside form

    Why not just make a keyboard hook? That way you don't need a timer and it's easier to process instead of havng to use an api to create a hotkey and then subclassing the window....

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Oct 2007
    Posts
    133

    Re: Keypress outside form

    How do i do a hook and what does it do?
    VB version: Visual Studio 2008-Professional

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Oct 2007
    Posts
    133

    Re: Keypress outside form

    And i looked at the link iprank gave, thats how i got to the problem above of not understanding it. My slow mind wasnt understanding it all :-p
    VB version: Visual Studio 2008-Professional

  13. #13
    Fanatic Member
    Join Date
    Jan 2007
    Location
    Middletown, CT
    Posts
    948

    Re: Keypress outside form

    To understand how a keyboard hook works you have to understand that windows communicates with programs and devices by a series of messages. Everything that happens in windows is message driven. There are messages for mouse events, such as the mouse moving, a button clicking, or the scroll wheel being rolled. There are messages for windows built-in commands, such as the command that windows sends to a control to have it re-paint itself of the screen. There are also keyboard messages that windows sends when a key is pressed, released, held, etc. These messages are known as a message stream (because they come in and out like a stream). Hooking is the process by which one hooks into that stream and re-routes the signal to your program. In the program is a function that processes those messages, called a callback function. This works kind of like you see the crooks do on tv all the time with security cameras. They tap into the stream of camera data, intercept it, manipulate it, and send it back to whoever is watching the cameras on screen. With a keyboard hook, the keyboard message stream is intercepted and sent to a function in your program. You then read it, process it, and send it along further down the stream. In your case you would just be trying to see when the letter E was pressed. So in your function you'd see if the message being sent through the stream was the WM_KEYDOWN message. If it was you'd then look into which key was pressed. The key would be the virtual keycode constant for E. You would then run whatever code you wanted to run when the letter e was pressed in that hooked function.

    Look into SetWindowsHookEx and UnhookWindowsHook in the MSDN library.
    Just a fair warning, it's evident that you've never used hooks before. Often times beginners will crash VB when trying to hook. This is fine, as you can always re-open your project. Just make sure you save it first.
    If VB crashes it's because you did something wrong with the coding for the hook. Remember any time you use the API (which is where those two functions are located), you're calling functions built into windows itself. This means you have to make sure everything is perfect, or something will crash.
    Last edited by drag0n_45; Jul 22nd, 2008 at 12:12 PM.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Oct 2007
    Posts
    133

    Re: Keypress outside form

    Thanks for the great explanation dragon, i will look into this
    VB version: Visual Studio 2008-Professional

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