|
-
Jul 17th, 2008, 09:43 AM
#1
Thread Starter
Addicted Member
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
-
Jul 17th, 2008, 09:47 AM
#2
Thread Starter
Addicted Member
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
-
Jul 20th, 2008, 11:15 AM
#3
Re: Keypress outside form
Take a look at the RegisterHotKey API.
-
Jul 21st, 2008, 09:01 AM
#4
Thread Starter
Addicted Member
Re: Keypress outside form
 Originally Posted by iPrank
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
-
Jul 21st, 2008, 01:04 PM
#5
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.
-
Jul 21st, 2008, 01:12 PM
#6
Thread Starter
Addicted Member
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
-
Jul 21st, 2008, 01:21 PM
#7
Re: Keypress outside form
 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
-
Jul 21st, 2008, 01:15 PM
#8
Thread Starter
Addicted Member
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
-
Jul 21st, 2008, 01:28 PM
#9
Re: Keypress outside form
 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
-
Jul 22nd, 2008, 10:07 AM
#10
Fanatic Member
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....
-
Jul 22nd, 2008, 10:46 AM
#11
Thread Starter
Addicted Member
Re: Keypress outside form
How do i do a hook and what does it do?
VB version: Visual Studio 2008-Professional
-
Jul 22nd, 2008, 10:47 AM
#12
Thread Starter
Addicted Member
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
-
Jul 22nd, 2008, 12:07 PM
#13
Fanatic Member
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.
-
Jul 22nd, 2008, 02:21 PM
#14
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|