Send Windows key keypress to application

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!

+4
source share
3 answers

If you need full integration with the operating system, you can connect to the kernel input functions using PInvoke.

What you are looking for is the explorer.exe Windows API function, described in detail here:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms646309%28v=vs.85%29.aspx

Using PInvoke, you can call this C ++ function

 [DllImport("user32.dll")] private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc); [DllImport("user32.dll")] private static extern bool UnregisterHotKey(IntPtr hWnd, int id); public const int MOD_WIN = 0x00000008; public const int KEY_W = 0x00000057 public static void RegisterKeys() { RegisterHotKey((IntPtr)this.Handle, 1, MOD_WIN, KEY_W); } protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == 0x0312) this.Visible = !this.Visible; } 
+3
source

I would also suggest taking a look at Auto Hotkey: http://www.autohotkey.com/ . If all you need is sending the window forward when you press a key, you can find it faster and write a script to do this (only 1 or 2 lines) than write something in C # or VB.

* edit * As for pressing a button automatically, if it is set as the default button, you can simply send the press of the enter key. Otherwise, you may need to send a mouse click even to a window. Not particularly difficult once you have completed the first part.

#W::MsgBox "This is a message box. You just need to use something like send to send keystrokes and other commands to the window you want to control, instead of the this message box."

+2
source

You can use Windows SendKeys . If you look at this link, you will see the codes and syntax for using the ctrl, alt or shift keys. I think you can send these keys to your application to simulate a shortcut.

+1
source

All Articles