I want to dissable window key which open start menu, in my WPF desktop application. The purpose is for security feature that user can not do any work while opening the application the only way to do other work is to close the application. I have tried command bindings and key hooks and OnPreviewKeydown and OnPreviewKeyUp but none of the work.
Is it possible to dissable the win key in WPF. If yes please help with an example.
I tried so far.
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
if (e.Key == Key.System)
{
e.Handled = true;
return;
}
if (e.Key == Key.LWin||e.Key==Key.RWin)
{
e.Handled = true;
return;
}
}
and============================================================
<Window.CommandBindings>
<CommandBinding Command="NotACommand"/>
<CommandBinding Command="Refresh" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed"/>
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Command="NotACommand" Key="RWin"/>
<KeyBinding Command="NotACommand" Key="LWin"/>
</Window.InputBindings>
and============================================================
using System;
public class globalKeyboardHook
{
#region Constant, Structure and Delegate Definitions
/// <summary>
/// defines the callback type for the hook
/// </summary>
public delegate int keyboardHookProc(int code, int wParam, ref keyboardHookStruct lParam);
public struct keyboardHookStruct
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}
const int WH_KEYBOARD_LL = 13;
const int WM_KEYDOWN = 0x100;
const int WM_KEYUP = 0x101;
const int WM_SYSKEYDOWN = 0x104;
const int WM_SYSKEYUP = 0x105;
#endregion
#region Instance Variables
/// <summary>
/// The collections of keys to watch for
/// </summary>
public List<Key> HookedKeys = new List<Key>();
/// <summary>
/// Handle to the hook, need this to unhook and call the next hook
/// </summary>
IntPtr hhook = IntPtr.Zero;
#endregion
#region Events
/// <summary>
/// Occurs when one of the hooked keys is pressed
/// </summary>
public event KeyEventHandler KeyDown;
/// <summary>
/// Occurs when one of the hooked keys is released
/// </summary>
public event KeyEventHandler KeyUp;
#endregion
#region Constructors and Destructors
/// <summary>
/// Initializes a new instance of the <see cref="globalKeyboardHook"/> class and installs the keyboard hook.
/// </summary>
public globalKeyboardHook()
{
hook();
}
/// <summary>
/// distructor of hook
/// </summary>
~globalKeyboardHook()
{
unhook();
}
#endregion
#region Public Methods
/// <summary>
/// Installs the global hook
/// </summary>
public void hook()
{
//keyboardHookProc hook = new keyboardHookProc(hookProc);
IntPtr hInstance = LoadLibrary("User32");
hhook = SetWindowsHookExA(WH_KEYBOARD_LL, hookProc, hInstance, 0);
}
/// <summary>
/// Uninstalls the global hook
/// </summary>
public void unhook()
{
UnhookWindowsHookEx(hhook);
}
/// <summary>
/// The callback for the keyboard hook
/// </summary>
/// <param name="code">The hook code, if it isn't >= 0, the function shouldn't do anyting</param>
/// <param name="wParam">The event type</param>
/// <param name="lParam">The keyhook event information</param>
/// <returns></returns>
public int hookProc(int code, int wParam, ref keyboardHookStruct lParam)
{
if (code >= 0)
{
Key key = (Key)lParam.vkCode;
if (HookedKeys.Contains(key))
{
//KeyEventArgs kea = new KeyEventArgs(key);
//if ((wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) && (KeyDown != null))
//{
//KeyDown(this, kea) ;
//}
//else if ((wParam == WM_KEYUP || wParam == WM_SYSKEYUP) && (KeyUp != null))
//{
///KeyUp(this, kea);
//}
//if (kea.Handled)
return 1;
}
}
return CallNextHookEx(hhook, code, wParam, ref lParam);
}
#endregion
#region DLL imports
/// <summary>
/// description: Sets the windows hook, do the desired event, one of hInstance or threadId must be non-null
/// </summary>
/// <param name="idHook"></param>
/// <param name="callback"></param>
/// <param name="hInstance"></param>
/// <param name="threadId"></param>
/// <returns></returns>
[DllImport("user32.dll")]
static extern IntPtr SetWindowsHookExA(int idHook, keyboardHookProc callback, IntPtr hInstance, uint threadId);
/// <summary>
/// Unhooks the windows hook.
/// </summary>
/// <param name="hInstance">The hook handle that was returned from SetWindowsHookEx</param>
/// <returns>True if successful, false otherwise</returns>
[DllImport("user32.dll")]
static extern bool UnhookWindowsHookEx(IntPtr hInstance);
/// <summary>
/// Calls the next hook.
/// </summary>
/// <param name="idHook">The hook id</param>
/// <param name="nCode">The hook code</param>
/// <param name="wParam">The wparam.</param>
/// <param name="lParam">The lparam.</param>
/// <returns></returns>
[DllImport("user32.dll")]
static extern int CallNextHookEx(IntPtr idHook, int nCode, int wParam, ref keyboardHookStruct lParam);
/// <summary>
/// Loads the library.
/// </summary>
/// <param name="lpFileName">Name of the library</param>
/// <returns>A handle to the library</returns>
[DllImport("kernel32.dll")]
static extern IntPtr LoadLibrary(string lpFileName);
#endregion
}
private void implimentHook()
//{
// try
// {
// globalHook.HookedKeys.Add(Key.LeftCtrl);
// globalHook.HookedKeys.Add(Key.RightCtrl);
// globalHook.HookedKeys.Add(Key.LWin);
// globalHook.HookedKeys.Add(Key.RWin);
// globalHook.HookedKeys.Add(Key.RightShift);
// globalHook.HookedKeys.Add(Key.LeftShift);
// globalHook.HookedKeys.Add(Key.LeftAlt);
// globalHook.HookedKeys.Add(Key.RightAlt);
// globalHook.HookedKeys.Add(Key.F1);
// globalHook.HookedKeys.Add(Key.F2);
// globalHook.HookedKeys.Add(Key.F3);
// globalHook.HookedKeys.Add(Key.F4);
// globalHook.HookedKeys.Add(Key.F5);
// globalHook.HookedKeys.Add(Key.F6);
// globalHook.HookedKeys.Add(Key.F7);
// globalHook.HookedKeys.Add(Key.F8);
// globalHook.HookedKeys.Add(Key.F9);
// globalHook.HookedKeys.Add(Key.F10);
// globalHook.HookedKeys.Add(Key.F11);
// globalHook.HookedKeys.Add(Key.F12);
// globalHook.KeyDown += new KeyEventHandler(globalHook_KeyDown);
// //globalHook.KeyUp += new KeyEventHandler(globalHook_KeyUp);
// Security.Keyboard.globalKeyboardHook.keyboardHookProc DelegatehookProc = new Security.Keyboard.globalKeyboardHook.keyboardHookProc(globalHook.hookProc);
// globalHook.hook();
// }
// catch (Exception ex)
// {
// MessageBox.Show("here");
// }
//}
//void globalHook_KeyUp(object sender, KeyEventArgs e)
//{
// e.Handled = true;
//}
//void globalHook_KeyDown(object sender, KeyEventArgs e)
//{
// e.Handled = true;
//}
}