Kill key strokes while hooked
-
Ok, I am using a global hook, and I am wondering how I can keep the keys from sending to the active program. I searched around the net and CodeProject and I just can't figure it out. I'm not terribly familiar with WindowsAPI stuff. Thanks in advance. -- modified at 19:33 Friday 17th February, 2006
-
Ok, I am using a global hook, and I am wondering how I can keep the keys from sending to the active program. I searched around the net and CodeProject and I just can't figure it out. I'm not terribly familiar with WindowsAPI stuff. Thanks in advance. -- modified at 19:33 Friday 17th February, 2006
You just figured it out? So, what is your problem? :) -- modified at 19:23 Friday 17th February, 2006
-
You just figured it out? So, what is your problem? :) -- modified at 19:23 Friday 17th February, 2006
Just *can't* figure out. Why is it that we always leave out the mosti mportant words? haha.
-
Just *can't* figure out. Why is it that we always leave out the mosti mportant words? haha.
This might help: http://msdn2.microsoft.com/en-us/library/system.windows.forms.application.addmessagefilter.aspx[^]
static void Main() { Application.AddMessageFilter(new TestMessageFilter()); Application.Run(new Form1()); } public class TestMessageFilter : IMessageFilter { public bool PreFilterMessage(ref Message m) { const int WM_KEYDOWN= 0x0100; // Blocks all the messages relating to the left mouse button. if (m.Msg==WM_KEYDOWN) { MessageBox.Show("KeyDown hitted"); } return false; } }
-- modified at 19:54 Friday 17th February, 2006 -
Ok, I am using a global hook, and I am wondering how I can keep the keys from sending to the active program. I searched around the net and CodeProject and I just can't figure it out. I'm not terribly familiar with WindowsAPI stuff. Thanks in advance. -- modified at 19:33 Friday 17th February, 2006
What type of hook are you using? A really global hook that captures all keyboard events from the whole system, or a hook that only applies to one application? Maybe you can show us a bit of code?
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick
|| Fold With Us! || Pensieve || VG.Net ||
-
What type of hook are you using? A really global hook that captures all keyboard events from the whole system, or a hook that only applies to one application? Maybe you can show us a bit of code?
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick
|| Fold With Us! || Pensieve || VG.Net ||
It is a global keyboard hook. I am using the Kennedy.ManagedHooks DLL from this site. I enable it with a hotkey, and disable it the same way.
public Form1() { // Alt = 1, Ctrl = 2, Shift = 4, Win = 8 Form1.RegisterHotKey(this.Handle,this.GetType().GetHashCode(), 8, (int)'P'); InitializeComponent(); keyboardHook = new Kennedy.ManagedHooks.KeyboardTracking(); keyboardHook.KeyboardEvent += new Kennedy.ManagedHooks.KeyboardTracking.KeyboardEventHandler(keyboardHook_KeyboardEventExt); } protected override void WndProc(ref Message m) { if (m.Msg == 0x0312) if (keyboardHook.IsHooked) { keyboardHook.UninstallHook(); input = false; textBox4.Text = ""; } else { textBox4.Text = ""; keyboardHook.InstallHook(); input = true; } base.WndProc(ref m); } private void keyboardHook_KeyboardEventExt(Kennedy.ManagedHooks.KeyboardEvents kEvent, Keys key) { bool shi; if(keyboardHook.ShiftPressed == true) shi = true; else shi = false; if(kEvent == KeyboardEvents.KeyDown){ string msg; //snipped out some key parsing junk. if(shi == true) msg = key.ToString(); else msg = key.ToString().ToLower(); textBox4.Text += msg; } }
I think that covers all the relevant code from my end of the application. If there are any questions about what anything does more specifically, I can answer those. The Hotkey works fine, and I am planning to add the message filter or whatever when it hotkeys in and remove it when they hotkey out of the hook, obviously. Thanks for the help thus far. -
It is a global keyboard hook. I am using the Kennedy.ManagedHooks DLL from this site. I enable it with a hotkey, and disable it the same way.
public Form1() { // Alt = 1, Ctrl = 2, Shift = 4, Win = 8 Form1.RegisterHotKey(this.Handle,this.GetType().GetHashCode(), 8, (int)'P'); InitializeComponent(); keyboardHook = new Kennedy.ManagedHooks.KeyboardTracking(); keyboardHook.KeyboardEvent += new Kennedy.ManagedHooks.KeyboardTracking.KeyboardEventHandler(keyboardHook_KeyboardEventExt); } protected override void WndProc(ref Message m) { if (m.Msg == 0x0312) if (keyboardHook.IsHooked) { keyboardHook.UninstallHook(); input = false; textBox4.Text = ""; } else { textBox4.Text = ""; keyboardHook.InstallHook(); input = true; } base.WndProc(ref m); } private void keyboardHook_KeyboardEventExt(Kennedy.ManagedHooks.KeyboardEvents kEvent, Keys key) { bool shi; if(keyboardHook.ShiftPressed == true) shi = true; else shi = false; if(kEvent == KeyboardEvents.KeyDown){ string msg; //snipped out some key parsing junk. if(shi == true) msg = key.ToString(); else msg = key.ToString().ToLower(); textBox4.Text += msg; } }
I think that covers all the relevant code from my end of the application. If there are any questions about what anything does more specifically, I can answer those. The Hotkey works fine, and I am planning to add the message filter or whatever when it hotkeys in and remove it when they hotkey out of the hook, obviously. Thanks for the help thus far.Well, in the "real" global hook implementation I did in C++, years ago, the solution is simple. You just don't pass the keystroke message up the hook chain. But I don't see a way to do that, or more to the point NOT do that, using this library. At least it's not obvious in the code you've posted. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
Well, in the "real" global hook implementation I did in C++, years ago, the solution is simple. You just don't pass the keystroke message up the hook chain. But I don't see a way to do that, or more to the point NOT do that, using this library. At least it's not obvious in the code you've posted. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
Also, I am using WH_KEYBOARD_LL to hook, if that helps out more.
-
Also, I am using WH_KEYBOARD_LL to hook, if that helps out more.
Ok, I am just going to use blockinput() I think. It blocks the mouse too, but oh well.
-
Also, I am using WH_KEYBOARD_LL to hook, if that helps out more.
All you need to do to eat the key message is NOT call the
CallNextHookEx
function in your KeyboardProc handler. It's that simple... LowLevelKeyboardProc Function[^] RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome -- modified at 9:33 Saturday 18th February, 2006 -
All you need to do to eat the key message is NOT call the
CallNextHookEx
function in your KeyboardProc handler. It's that simple... LowLevelKeyboardProc Function[^] RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome -- modified at 9:33 Saturday 18th February, 2006Thanks! I'll go mess around with my hooking code.