WPF C#,应用程序中的Windows Dissabling键 [英] WPF C#, Dissabling Windows key in application

查看:105
本文介绍了WPF C#,应用程序中的Windows Dissabling键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在WPF桌面应用程序中禁用打开开始菜单的窗口键.目的是为了实现安全功能,即用户在打开应用程序时无法执行任何工作,而执行其他工作的唯一方法是关闭应用程序.我尝试了命令绑定和键挂钩以及OnPreviewKeydown和OnPreviewKeyUp,但是没有任何工作.
是否有可能在WPF中禁用Win键.如果是,请提供示例.

我到目前为止已经尝试过了.

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) //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============================================================

<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;
        //}
}

推荐答案

您可能对从C#挂钩的低级Windows API可以停止不必要的击键 [ ^ ]处理Windows的开始"键
you might be interested in Low-level Windows API hooks from C# to stop unwanted keystrokes[^] it deals with the Windows Start Key


此处 [ ^ ]是有关CP的文章,显示了在运行应用程序时锁定Windows桌面的多种方法.

希望对您有帮助
Here[^] is an article on CP that shows multiple ways of locking the Windows desktop while running your application.

Hope this helps


这篇关于WPF C#,应用程序中的Windows Dissabling键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆