Keys
Hi I seem to be running into a bit of trouble. I found found the function Sendkeys to beable to send a key and also repeatedly send a key to the computer. But I am looking for a function that will tell the computer a key is being pressed down, then i guess sleep for 3 seconds, and then released after. Is this possible? I have look through a lot of stuff on google but have found nothing. So if anyone of you can help that would be awesome!
Jan MontanoPosted Jan 29, 2008, 1:45 AM
This article is about Global System Hooks in .Net. You could filter it to use only keyboard hooks instead.
Otherwise, if you're monitoring keyboard activity within your application (winform) only, then a simple Keypress event would do it for you.
Hope this helps
Regards,
Jan
Scott LyslePosted Jan 29, 2008, 1:40 AM
Something like this?
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace SleepOnKeyStrokes { public partial class Form1 : Form { #region Dynamic Link Library Imports [DllImport("user32.dll")] private static extern int RegisterHotKey(IntPtr hwnd, int id, int fsModifiers, int vk); [DllImport("user32.dll")] private static extern int UnregisterHotKey(IntPtr hwnd, int id); #endregion #region Modifier Constants and Variables // Constants for modifier keys private const int USE_ALT = 1; private const int USE_CTRL = 2; private const int USE_SHIFT = 4; private const int USE_WIN = 8; // Hot key ID tracker short mHotKeyId = 0; #endregion public Form1() { InitializeComponent(); // Setup ALT+F7- Sleep for three seconds RegisterGlobalHotKey(Keys.F7, USE_ALT); } private void button1_Click(object sender, EventArgs e) { Application.Exit(); } private void RegisterGlobalHotKey(Keys hotkey, int modifiers) { try { // increment the hot key value - we are just identifying // them with a sequential number since we have multiples mHotKeyId++; if (mHotKeyId > 0) { // register the hot key combination if (RegisterHotKey(this.Handle, mHotKeyId, modifiers, Convert.ToInt16(hotkey)) == 0) { // tell the user which combination failed to register - // this is useful to you, not an end user; the end user // should never see this application run MessageBox.Show("Error: " + mHotKeyId.ToString() + " - " + Marshal.GetLastWin32Error().ToString(), "Hot Key Registration"); } } } catch { // clean up if hotkey registration failed - // nothing works if it fails UnregisterGlobalHotKey(); } } private void UnregisterGlobalHotKey() { // loop through each hotkey id and // disable it for (int i = 0; i < mHotKeyId; i++) { UnregisterHotKey(this.Handle, i); } } protected override void WndProc(ref Message m) { base.WndProc(ref m); // if the message matches, // disregard it const int WM_HOTKEY = 0x312; if (m.Msg == WM_HOTKEY) { System.Threading.Thread.Sleep(3000); MessageBox.Show("Wake"); } } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { // unregister the hot keys or they will survive the application exit UnregisterGlobalHotKey(); } } }