Possible duplicate:
How to determine the current key pressed?
(EDIT: what is it for, this is not a duplicate ... not sure why it was voted as such)
I have a small timer application that we use in the office to track hours spent on a project. There is a start and stop button, as well as project and task fields. When we go for a break, for lunch and other things, we stop the timer for this project, and then restart it. This is a recurring task that usually involves digging up a window due to several other Windows, then the same after breaking.
What I want to do is assign WindowKey + W to the working timer application and make it not only attach the timer application to the front, but also focus it, but also switch Start / Stop.
I tried several searches, but I can not narrow down the examples to what I want. I know that you can open the properties of the Windows shortcut and assign a shortcut to run the program, and (I think?), If you already have this application, and it allows you to allow only one instance of the program will bring this program to the front ??? perhaps..
Anyway .. but this method will not accept WindowsKey as a valid key combination. And I donβt know if he can somehow transfer this key combination to the program.
I appreciate any help or direction here!
EDIT - Update message
Thanks @huadianz for your answer! I converted your code to VB:
Public Const MOD_WIN As Integer = &H8 Public Const KEY_W As Integer = &H57 <DllImport("user32.dll")> _ Public Shared Function RegisterHotKey(hWnd As IntPtr, id As Integer, fsModifiers As Integer, vlc As Integer) As Boolean End Function <DllImport("user32.dll")> _ Public Shared Function UnregisterHotKey(hWnd As IntPtr, id As Integer) As Boolean End Function Public Sub RegisterKeys() RegisterHotKey(Me.Handle, 1, MOD_WIN, KEY_W) End Sub Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) MyBase.WndProc(m) If (m.Msg = &H312) Then Me.TopMost = True Me.PlayPauseTimer() Me.TopMost = False End If End Sub
It is interesting to note that Me.BringToFront() would not really bring the application to the forefront in this scenario in Win7, rather than Me.Focus() . However, Me.TopMost = True worked, but it has a secondary effect of having the window always on top. Set it to True by switching the timer and then returning it to False , great!