Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            if (e.Key == Key.System) //this will catch alt-tab, alt-esc- alt-f4 etc. BUT NOT CONSISTANTLY!
            {
                e.Handled = true;
                return;
            }
            if (e.Key == Key.LWin||e.Key==Key.RWin)
            {
                e.Handled = true;
                return;
            }
        }


and============================================================

XML
<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============================================================

XML
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;
        //}
}
Posted
Updated 25-Aug-11 23:23pm
v2

you might be interested in Low-level Windows API hooks from C# to stop unwanted keystrokes[^] it deals with the Windows Start Key
 
Share this answer
 
Comments
Tiwari Avinash 27-Aug-11 10:22am    
i have tried these but none works for me.. thanks for ur help n time
Here[^] is an article on CP that shows multiple ways of locking the Windows desktop while running your application.

Hope this helps
 
Share this answer
 
Comments
Tiwari Avinash 27-Aug-11 10:23am    
i have tried these but none works for me.. thanks for ur help n time

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900