How can i disable LWin, RWin, Alt + F4...

Oct 26, 2011 at 4:11 PM
Edited Oct 26, 2011 at 4:11 PM

I added MouseKeyboardActivityMonitor.dll to my project and i want to disable keystroke combination Alt + F4 on my application.

How can i disable LWin, RWin, Alt + F4... using your library ?

Thanx for the answer !

Oct 26, 2011 at 6:16 PM

Following snipped disables WinKey + R combination.
There are two steps 1. Detect the combination 2. Disable further handling by setting e.Handled = true.
Hope it helps. 

private KeyboardHookListener m_KeyboardHookManager;
private bool m_LeftWinKeyIsDown;
private bool m_RightWinKeyIsDown;

private void TestForm_Load(object sender, EventArgs e)
{
    m_KeyboardHookManager = new KeyboardHookListener(new GlobalHooker());
    m_KeyboardHookManager.KeyDown += HookManager_KeyDown;
    m_KeyboardHookManager.KeyUp += HookManager_KeyUp;
    m_KeyboardHookManager.Enabled = true;
}

private void HookManager_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode ==Keys.LWin) m_LeftWinKeyIsDown = true;
    if (e.KeyCode ==Keys.RWin) m_RightWinKeyIsDown = true;
    if ((m_LeftWinKeyIsDown || m_RightWinKeyIsDown) && e.KeyCode == Keys.R) e.Handled = true;
}

private void HookManager_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.LWin) m_LeftWinKeyIsDown = false;
    if (e.KeyCode == Keys.RWin) m_RightWinKeyIsDown = false;
}
Oct 27, 2011 at 5:58 AM

Perhaps we should start a code snippets / examples section in the documentation?

Much of the comments section on CodeProject, where you've written examples can be easily used (or updated). If you think it's a good idea, I'll start on it.

Oct 27, 2011 at 8:11 AM

It is a great idea to introduce a HowTo section with all most needed code snippets.


By the way the case of intercepting particular key combination is a very common use case. 
I thought about a new class which allows to subscribe to any combination.
You pass the combination (as an array of args of type key) and became notified when it occurs.
In a callback you have also possibility to set Handled flag so that the further processing will be stopped.

Oct 27, 2011 at 10:29 AM

Thank so much :)

Oct 27, 2011 at 5:36 PM

@gmamaladze

I too had thought about something similar. Hotkeys essentially, as I'd said in the V3 Release announcement discussion. =)