Question KeyDown Event on another Window

joguizi

New member
Joined
Nov 9, 2010
Messages
2
Programming Experience
Beginner
Hi all. I am wondering if it was possible to, for exemple :
if I press F1 on another program like, internet explorer,
it will open music.
When I press F1 on my Windows Form(My program),
It work fine and it play my music but when i'm on internet explorer or anything else it doesn't work and I am wondering if I can.
I hope you will understand because it's hard to explain :p

Thanks in advance :D

joguizi
 
It's possible to register a hotkey with the OS so that your app will be notified whenever that key combination is pressed, whether it has focus or not. Using F1 as the hotkey is a very bad choice though, because that is the standard Windows shortcut for Help. If you want to register a hotkey then you should always include at least one, preferably two, modifier keys. Basically, you should strive to use a key combination that other applications are unlikely to respond to when they have focus, e.g. Ctrl+Shift+Q.

To register your hotkey, you need to call the RegisterHotKey Windows API function. Search the web and you will find multiple examples. Make sure you call UnregisterHotKey as well when your app closes.
 
Ok thanks.
After much research, I still do not find how to do with the Operating System (OS).
I found this codes but it still doing it when the Form is on top.

(With a Timer)
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Dim value As Boolean = My.Computer.Keyboard.CtrlKeyDown
(Public Class Form1....ignore.......)

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If My.Computer.Keyboard.CtrlKeyDown Then
MsgBox("CTRL key down")
End If
End Sub
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
And
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Private Declare Sub key_event Lib "user32" (ByVal dwflags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cbuttons As Long, ByVal dwExtraInfo As Long)
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer
(Public Class Form1....ignore.......)

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Dim hotkey As Boolean
hotkey = GetAsyncKeyState(Keys.F1)
If hotkey = True Then Timer1.Start()
Dim hotkey1 As Boolean
hotkey1 = GetAsyncKeyState(Keys.F2)
If hotkey1 = True Then Timer1.Stop()
End Sub
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Can you explain me OS and API function? Or give me an example of code :)

Please help me :(.

Thanks in advance :D
Joguizi
 
Last edited:
Create a timer and use this function and import GetAsyncKeyState
VB.NET:
  Function GetKeyDown(ByVal Vkey As Int32) As Boolean
        If GetAsyncKeyState(Vkey) = -32767 Then
            Return True
        Else
            Return False
        End If
    End Function

then in your timer.tick event

VB.NET:
if GetKeyDown(keys.f1) = true and MusicOpen = false then
MusicOpen = True
'code to play your music
end if
 
Create a timer and use this function and import GetAsyncKeyState
VB.NET:
  Function GetKeyDown(ByVal Vkey As Int32) As Boolean
        If GetAsyncKeyState(Vkey) = -32767 Then
            Return True
        Else
            Return False
        End If
    End Function

then in your timer.tick event

VB.NET:
if GetKeyDown(keys.f1) = true and MusicOpen = false then
MusicOpen = True
'code to play your music
end if

That's a very bad idea. If you have the choice between a notification and constant polling, always choose the notification. That's exactly what RegisterHotKey does: provides a notification when a specific key combination is pressed. It's like the difference between sitting in the back seat and asking the driver "area we there yet" over and over or just waiting for the driver to tell you "we're there". The former takes a lot more effort and doesn't get you there any sooner. Again, polling is a very bad idea unless it's absolutely necessary, which it definitely isn't in this case.
 
Yeah , nice example really learn a lot , I have had tried to learn this in previous years but I can't , is there so much api to get this done...

Can you give me an example how to RegisterHotKey .

That's a very bad idea. If you have the choice between a notification and constant polling, always choose the notification. That's exactly what RegisterHotKey does: provides a notification when a specific key combination is pressed. It's like the difference between sitting in the back seat and asking the driver "area we there yet" over and over or just waiting for the driver to tell you "we're there". The former takes a lot more effort and doesn't get you there any sooner. Again, polling is a very bad idea unless it's absolutely necessary, which it definitely isn't in this case.
 
Yeah , nice example really learn a lot , I have had tried to learn this in previous years but I can't , is there so much api to get this done...

Can you give me an example how to RegisterHotKey .

Any example I provide will just be based on examples already out there on the web, but you can find those for yourself so there's no point my reproducing them here. I've never used RegisterHotKey in an application myself. I only know how to use it from reading other people's examples on the web, which you can do just as well as I can. A more general understanding of platform invoke is helpful too, so that you can understand mapping of managed parameters to unamanaged parameters, but that can only come with practice.
 
Back
Top