Hot Keys

ImDaFrEaK

Well-known member
Joined
Jan 7, 2006
Messages
416
Location
California
Programming Experience
5-10
I have written a personal mp3 player called G3 Player. It works great.... I would be happy to share the G3 Player class for use in anyones form. There are many many methods, property's, and events to take advantage of in it. ACtually I would like to share the install of it for critism and advice. Anyways.....

My conflict is I want to add hotkeys to the player. I want to be able to switch songs or however while playing my starcraft. I really just need to learn how to build hotkeys for a particular application even when the app dosn't have the focus. Can anyone help or even get me on the right track for building hotkeys? A demo for one acting hotkey would be great for now. Thanks :)
 
Thanks guys.... I really appreciate the quick response and effort going out of your way to find the link also. However, I am a lil too nieve to use that information. I even tried copy and pasting it and messing around for a while. I finally gave up. Nothing worked. I anyone could explain in better detail what's going on inside that link above I may grasp it better. This may be reaching too far beyong my programming skills thus far but if I can just get the main idea out of it all and even cut and paste some of it then I would be happy. I'll just make a class just for Universal key events or 'keyboard hooking' and use it throughout until i learn more. If anyone wants to give me a quick tutorial or lector or whatever, even a book name that defines these points specifically then I would be very thankful. I am again thankful for the responses I have already gotten. I will continue to look abroad until it either clicks in or someone teaches me. Thanks :)
 
hope I didn't miss something in this copy/paste, should be complete example
VB.NET:
Public Class Form1
  Inherits System.Windows.Forms.Form
 
' windows forms generated code
 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  SetupHotKey()
End Sub
 
#Region "HotKey"
Dim AtomIds(0) As Int32
 
Private Sub SetupHotKey()
  'register hotkeys
  Dim fsModifiers, vkey As Int32
  fsModifiers = Win32.HotkeyModifierFlags.MOD_WIN
  'hotkey0 win-t 'mytest'
  vkey = Strings.Asc("T")
  AtomIds(0) = Win32.GlobalAddAtom("JHmytest")
  If Win32.RegisterHotKey(Me.Handle, AtomIds(0), fsModifiers, vkey) = False Then
    MsgBox("error registering hotkey 0")
  End If
End Sub
 
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
  Select Case (m.Msg)
  Case Win32.WM_HOTKEY
    If m.WParam.ToInt32 = AtomIds(0) Then 'it's my hotkey 'mytest'
      'do something
      myHotkeyaction()
      'set result, message handled
      m.Result = New IntPtr(1)
    End If
  End Select
  MyBase.WndProc(m)
End Sub
 
Sub myHotkeyaction()
  'something to do
End Sub
 
Private Sub DeHotKey()
  Win32.UnregisterHotKey(Me.Handle, AtomIds(0))
  Win32.GlobalDeleteAtom(AtomIds(0))
End Sub
 
#End Region 'Hotkey
 
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
  DeHotKey()
End Sub
 
End Class
 
Friend Class Win32
  Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal Id As Int32, ByVal fsModifiers As Int32, ByVal vkey As Int32) As Boolean
  Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal Id As Int32) As Boolean
  Public Enum HotkeyModifierFlags
    MOD_ALT = &H1
    MOD_CONTROL = &H2
    MOD_SHIFT = &H4
    MOD_WIN = &H8
  End Enum
  Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpString As String) As Int32
  Declare Function GlobalDeleteAtom Lib "kernel32" (ByVal nAtom As Int32) As Int32
  Public Const WM_HOTKEY As Integer = &H312
End Class 'Win32
 
I pasted that code into a new project and the code works nicely. Thanks. :)

Do you know how I would add combinations of the hotkey modifiers? Like, if I wanted to know when the user pressed CONTROL + ALT + SHIFT + N.
 
You can combine them MOD_ALT+MOD_CONTROL
 
I finally did figure this out how to get system hot keys a long time ago but never came back to the post to talk about it. I had to make a call to an API and crap but basically what became of it is a class that handles hotkeys and fires an event no matter what key is pressed or combination. You simply look at the keys argument i pass with the event to see what keys were fired. You can use a Select Case or just look for one or a combination of keys. It's really easy to use if anyone is interested. I run this in my player all the time on my pc and I havn't had any problems thus far.

I have the .dll all set up for developer use and it works very well thus far.

Here is an example of using my System Wide HotKeys Class.


Note: This code is simply an example of a few different situations.
VB.NET:
[SIZE=2][COLOR=#0000ff]Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]WithEvents[/COLOR][/SIZE][SIZE=2] HotKeys [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] TripleG.SystemHotKeys.HookTheKeyBoard[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] G3P_Main_Form_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load[/SIZE]
 
[SIZE=2][COLOR=#008000][COLOR=black]HotKeys.HookKeyboard()[/COLOR][/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
 
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] HotKeys_HookedKeys([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] Keys [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] TripleG.SystemHotKeys.HookedKeyEventArguments) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] HotKeys.HookedKeys[/SIZE]
 
[SIZE=2][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Keys.LeftAltKeyIsDown [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]   'some code[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Select [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] Keys.KeyCode[/SIZE]
[SIZE=2][COLOR=#0000ff]   Case[/COLOR][/SIZE][SIZE=2] Windows.Forms.Keys.A[/SIZE]
[SIZE=2][COLOR=#008000]         'some code[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]   Case[/COLOR][/SIZE][SIZE=2] Windows.Forms.Keys.BrowserBack[/SIZE]
[SIZE=2][COLOR=#008000]         'some code[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Select[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Keys.KeyCode = Windows.Forms.Keys.Delete [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]   Keys.Handled = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
Last edited:
Sorry to bring this old thread back up, i was just wondering if you managed to get it to read the media keys from keyboards?

Also would it be possible to get the code for your G3 player?

Thanks.
 
Yes it reads the media keys from the keyboard.

No, sorry I can't share the code for the Player. You can get the player in it's simplicity from my website and check it out.

I can help you build a player.

Also, I am re-wiring the .dll and others to offer more flexability so the currents ones are not available for download. For example one change will be to the event above becoming Global_KeyDown and Global_KeyUp Events with the same properties pretty much. I have easier ways to use the .dll and so I want to offer this to others as well.
 
Thats fair enough mate. All I am after is the ability to read media keys from a keyboard, could you share that bit of the code, or explain how to do this?

EDIT: I have tried your player and it doesn't read my media keys on my keyboard (Logitech keyboard)....they work fine in WMP and other media software.
 
I have updated the player. There was a glitch reading media keys however this is not what you are experiencing. Logitech uses their own software to control Media programs. If you un-install the Logitech Software you will notice the keyboard still works but the media keys and other features it offers do not. I have a Blue Tooth Logitech Keyboard myself and the Media Keys do not work for my player either. I have to install my Gateway Normal Keyboard to test the Media Keys. Logitechs media keys apparently are not sending the same message that Media Keys send. I think Logitechs software is catching the keys and doing what they want with them like automatically open your Media Player and all. Either way, Media Keys are caught in the player and in the .dll I wrote. I am looking into figuring out what Logitech is doing but I think I will have to register my software with them in order to have their software interact with my Media Software. Not sure. It's all in study but be sure to know the Media Keys are being caught and are no different than catching any other keyboard events. Thanks for the feedback though, I did find a glitch while trying to figure this out and hopefully fixed it.

What you think about the player mate?

O, if you can catch any keyboard events then you use the same process to catch Media Keys. It's the same idea. How far along are you with catching keyboard events? I can help you with this.
 
I don't think it has anything to do with being registered with the Logitech software personally, because you can get alot of media players that work with the logitech keyboard.

What i think it is, and it might be worth looking into this:

I think there is a kind of registry key (maybe something else) or something saying that this is media software. What logitech software will do is check for all the media software on the pc and then it will send the keys to the appropriate programs. Thats my opinion anyway's.

In regards to your player, personally, I am not a fan. The skin doesn't appeal to my taste and personally i don't think its very user friendly. A media program should be one that you can pick up without any tuition on how to use it (IMO), and I was completely stuck pretty much straight away.

====

I am unable to read any sort of keys, I don't know how to do globalhooks...i did and then i forgot :/
 
Right on. I'll look into the Registry thing. That sounds realistic to me.

Thanks for the feedback on the player. Sorry it's not user friendly but there are a lot of neat features that I think are useful. Please Please and maybe not on this forum but please send me some information on how I can make it more appealing to you. The more I adjust it to fit various people the more popular it becomes. So please e-mail me, private message, contact from website anyway you want but please give me some way to make it more appealing to you. I myself have put little creativity of my own into this player, it has been customized to all the feedback i get from various people. Basically, the more reponse I get, the better it becomes. The key though is I want to keep it simple. I want a simple easy player with great sound quality.

As far as Global Keys. I bought a book Titled "Subclassing & Hooking" with Visual Basic. It's published by O'REILLY and written by Steven Teilhet. That was how I got started on hooking and actually understanding what I was doing. Afterwards I googled many articles and read a lot of Microsoft's articles. What I have written looks very little like examples I've seen and all but it works. If you want a real basic keyboard hook I can help you write this but I reccommend understanding what is going on as well so that you can mold and bend it to your desire much easier.
 
Thanks for the response!

It seems that it may not be registery keys that are for the media keys, and If you look at this thread by me: http://vbdotnetforums.com/showthread.php?t=16311

It seems to be just different events, so It might be worth looking into that.

I will pm you some info on my opinion of your player later, I would use your website but it is impossible to use (lol sorry again, i don't mean to rip it out of you or your site)..,It might be worth looking into getting that properly done so the site is easy to use, look into xHTML & CSS for webdesign rather than photoshop.

If you could do a quick small example of global hooking that would be great.

Thanks.

EDIT:

Also if you have any articles on global hooking in vb, that would be good too because all i seem to get it C# code and I am not brilliant at that, although at one point i managed to adapt some C# code into VB.NET for global hooking, but i lost it :/
 
Back
Top