begin edit #2 ...
Here's a possible solution that does use the code from the 2007 CP article by StormySpike you used:
I modified the Form1 design-time view like this:
1. set the Dock Property of the lstLog ListBox to "None."
2. re-sized the lstLog Control to open-up room at the top of the Form.
3. added a CheckBox, checkBox1, with the Text: "Key Handle On/Off" at the top of the Form.
Here's the modified code for that articles's Form1.cs file:
using System;
using System.Windows.Forms;
using Utilities;
namespace key_preview
{
public partial class Form1 : Form
{
globalKeyboardHook myHook = new globalKeyboardHook();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
checkBox1.Checked = true;
myHook.HookedKeys.Add(Keys.F1);
myHook.HookedKeys.Add(Keys.F2);
myHook.HookedKeys.Add(Keys.F3);
myHook.HookedKeys.Add(Keys.F4);
myHook.HookedKeys.Add(Keys.F5);
myHook.HookedKeys.Add(Keys.F6);
myHook.HookedKeys.Add(Keys.F7);
myHook.HookedKeys.Add(Keys.F8);
myHook.HookedKeys.Add(Keys.F9);
myHook.HookedKeys.Add(Keys.F10);
myHook.HookedKeys.Add(Keys.F11);
myHook.HookedKeys.Add(Keys.F12);
myHook.KeyDown += myHook_KeyDown;
myHook.KeyUp += myHook_KeyUp;
}
private void myHook_KeyUp(object sender, KeyEventArgs e)
{
if (checkBox1.Checked && myHook.HookedKeys.Contains(e.KeyCode))
{
e.Handled = true;
return;
}
lstLog.Items.Add("Up\t" + e.KeyCode.ToString());
}
private void myHook_KeyDown(object sender, KeyEventArgs e)
{
if (checkBox1.Checked && myHook.HookedKeys.Contains(e.KeyCode))
{
switch(e.KeyCode)
{
case Keys.F1:
lstLog.Items.Add("handled F1\t" + e.KeyCode.ToString());
break;
case Keys.F12:
lstLog.Items.Add("handled F12\t" + e.KeyCode.ToString());
break;
}
e.Handled = true;
return;
}
lstLog.Items.Add("Key Down passed through\t" + e.KeyCode.ToString());
}
}
}
I tested this code in VS 2013 compiling with FrameWork 4.5. No problems in Win 8/64 checking, and un-checking, the CheckBox several times and testing the F1 and F12 keys.
end edit #2 ...
begin edit #1 ...
I took George Mamaladze's latest HookManager source + demo app [
^], opened it in VS 2013, let VS convert it to use FrameWork 4.5.
fyi: downloading and running George's latest
demo (v. 2) of his hook manager: consistently fails in Win 8, but his original demo works fine. I'll report the error details to George.
I verified that I could modify his KeyDown behavior, by altering one method, to stop handling the F1 key-down only, passing through all other key-downs:
private void HookManager_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.F1)
{
e.Handled = true;
return;
}
textBoxLog.AppendText(string.Format("KeyDown - {0}\n", e.KeyCode));
textBoxLog.ScrollToCaret();
}
I verified that checking and un-checking the CheckBox for 'KeyDown several times resulted in the F1 key being handled, or passed on to Windows as expected, worked properly.
This solution suggests that it is
not necessary to install and un-install the hook EventHandlers for the hook to work as expected.
end edit #2 ...
begin edit #1 ...
George's code
removes the EventHandler when the KeyDown intercept CheckBox is unchecked, and
installs/re-installs it when it is checked.
Hypothesis: you need to write an EventHandler for the CheckedChanged Event of the ComboBox, and un-install or install the EventHandler based on whether the CheckBox is checked, or not.
Whether you can modify the code you were working with originally (not George's solution) is something I haven't looked at.
end edit #1 ...
I suggest you switch to using George Mamaladze's Global Hook code here on CP which has been updated more recently than the code you are using now: [
^].
The nature of using global hook calls from C# is inherently complex, involving use of WinApi and Platform Invoke facilities. The functionality you require may not be simple, and if you are a beginner in C# this may be a problem.