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?
Re: Keypress outside form
Take a look at the RegisterHotKey API.
Re: Keypress outside form
Quote:
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
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
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".
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.
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 :D
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 :thumb:
:D
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....
Re: Keypress outside form
How do i do a hook and what does it do?
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
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.
Re: Keypress outside form
Thanks for the great explanation dragon, i will look into this :)