简体   繁体   中英

Suppress certain Windows keyboard shortcuts

I'm attempting to suppress some Windows keyboard shortcuts (eg ALT + TAB , LWIN / RWIN , ALT + F4 ) so my application can handle them elsehow (simulating keypresses on an external machine).

According to other SO questions and answers this is supposed to work:

        this.PreviewKeyDown += (s, e) => {
            if (e.Key == Key.LWin || e.Key == Key.RWin)
                e.Handled = true;
        };

Problem is, it doesn't work. Whenever I hit LWIN / RWIN , the Start menu still pops up which I want to suppress so my application alone can use it. In the above snippet, this refers to a WPF Window which was being focussed while testing this. (It should obviously only suppress the action once Window has focus.)

Any way to achieve what I'd like to achieve?

Thanks,

~Tgys

You can Hook all the Keyboard and even Mouse events to detect the source of input. In other words, if you use the Global Hook as mentioned in the link below, you can capture system events and either process them like normal events or suppress them.

You should have a look at this CodeProject article, Processing Global Mouse and Keyboard Hooks in C#

MSDN Reference :

A global hook monitors messages for all threads in the same desktop as the calling thread. A thread-specific hook monitors messages for only an individual thread. A global hook procedure can be called in the context of any application in the same desktop as the calling thread, so the procedure must be in a separate DLL module. A thread-specific hook procedure is called only in the context of the associated thread.

C# Code:

using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public partial class MainWindow : Window
{
    // Structure contain information about low-level keyboard input event
    [StructLayout(LayoutKind.Sequential)]
    private struct KBDLLHOOKSTRUCT
    {
        public Keys key;
        public int scanCode;
        public int flags;
        public int time;
        public IntPtr extra;
    }

    //System level functions to be used for hook and unhook keyboard input
    private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr SetWindowsHookEx(int id, LowLevelKeyboardProc callback, IntPtr hMod, uint dwThreadId);
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool UnhookWindowsHookEx(IntPtr hook);
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr CallNextHookEx(IntPtr hook, int nCode, IntPtr wp, IntPtr lp);
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr GetModuleHandle(string name);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern short GetAsyncKeyState(Keys key);

    //Declaring Global objects
    private IntPtr ptrHook;
    private LowLevelKeyboardProc objKeyboardProcess;

    private IntPtr CaptureKey(int nCode, IntPtr wp, IntPtr lp)
    {
        if (nCode >= 0)
        {
            KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lp, typeof(KBDLLHOOKSTRUCT));
            if (objKeyInfo.key == Keys.RWin || objKeyInfo.key == Keys.LWin) // Disabling Windows keys
            {
                return (IntPtr)1;
            }
        }
        return CallNextHookEx(ptrHook, nCode, wp, lp);
    }

    public MainWindow()
    {
        InitializeComponent();

        //Get Current Module
        ProcessModule objCurrentModule = Process.GetCurrentProcess().MainModule;
        //Assign callback function each time keyboard process
        objKeyboardProcess = new LowLevelKeyboardProc(CaptureKey);
        //Setting Hook of Keyboard Process for current module
        ptrHook = SetWindowsHookEx(13, objKeyboardProcess, GetModuleHandle(objCurrentModule.ModuleName), 0); 
    }
}

Other Good References:

  1. Low-level Windows API hooks from C# to stop unwanted keystrokes
  2. Disable Special Keys in Win App C#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM