Basically you want to track key events
of forms.
First, you can inherit
all your forms from base form (Form's KeyPreview
property should be True
), like;
public class BaseForm : Form
{
private static List<Keys> keyPress = new List<Keys>(); // to store the key values
public BaseForm()
{
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.BaseForm_KeyDown); // register event for all forms which inherits from BaseForm
}
private void BaseForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.ControlKey || e.KeyCode == Keys.C || e.KeyCode == Keys.V) // right now Control, C, V keys are included
{
if (keyPress.Any(x => x == e.KeyCode)) // If keypress list has the current key press so pattern is broken
{
keyPress.Clear(); //clear the list user should start again
}
else
{
keyPress.Add(e.KeyCode);
}
if (keyPress.Count > 2)
{
this.Controls["textBox1"].Enabled = true;
keyPress.Clear(); // clear the keyPress list
}
}
}
}
So by doing this, you don't need to register keydown event for all your forms.
Second, you can use global key hook. (More information click this) At the main thread enterance Low-LevelKeyboardHook
will be attached to the process and every key event will be catched at HookCallback
.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Linq;
namespace KeyHookTryApp
{
static class Program
{
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private static LowLevelKeyboardProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
private static Form1 f;
private static List<Keys> keyPress = new List<Keys>();
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
_hookID = SetHook(_proc);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
f = new Form1();
Application.Run(f);
UnhookWindowsHookEx(_hookID);
}
private static IntPtr SetHook(LowLevelKeyboardProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
GetModuleHandle(curModule.ModuleName), 0);
}
}
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
int vkCode = Marshal.ReadInt32(lParam);
Keys key = (Keys)vkCode;
if (nCode >= 0 && ((key == Keys.LControlKey || key == Keys.RControlKey) || key == Keys.C || key == Keys.V))
{
if (keyPress.Any(x => x == key))
{
keyPress.Clear();
}
else
{
keyPress.Add(key);
}
if (keyPress.Count > 2)
{
f.Controls["textBox1"].Enabled = true;
keyPress.Clear();
}
}
else if(keyPress.Count > 0)
{
keyPress.Clear();
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook,LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
}
}
If there are multiple forms. The form controls should be reachable from global, or the forms should have custom methods like EnableTextBoxes()
.