gpt4 book ai didi

c# - 在 Winforms 中捕获 WinKey+D 键序列

转载 作者:太空宇宙 更新时间:2023-11-03 13:03:02 27 4
gpt4 key购买 nike

我试图让我的应用程序始终显示在桌面级别。这意味着我的应用程序需要忽略 LWin+D 或 RWin+D 等键序列。我试图让它以这种方式工作:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (prefixSeen)
{
if (keyData == Keys.D)
{
MessageBox.Show("Got it!");
}
prefixSeen = false;
return true;
}
if (keyData == Keys.LWin)
{
prefixSeen = true;
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}

但它只捕获 RWin/LWin 按钮,没有 D 按钮。

我也曾尝试创建自己的消息过滤器,但我迷失了方向。所有这些messages和按位:

    public class KeystrokMessageFilter : System.Windows.Forms.IMessageFilter
{
public KeystrokMessageFilter() { }

public bool PreFilterMessage(ref Message m)
{
if ((m.Msg == 256 /*0x0100*/))
{
switch (((int)m.WParam) | ((int)Control.ModifierKeys))
{
case (int)(Keys.Control | Keys.Alt | Keys.K):
MessageBox.Show("You pressed ctrl + alt + k");
break;
case (int)(Keys.Control | Keys.C): MessageBox.Show("ctrl+c");
break;
case (int)(Keys.Control | Keys.V): MessageBox.Show("ctrl+v");
break;
case (int)Keys.Up: MessageBox.Show("You pressed up");
break;
}
}
return false;
}
}

Application.AddMessageFilter(keyStrokeMessageFilter);

那么,我该如何让我的应用程序捕捉/忽略 R/LWin+D?

最佳答案

此代码注册一个低级键盘 Hook ,并在按住 Windows 键时监听 D 按键。如果检测到这一点, Hook 会简单地忽略按键。否则,它会转发按键。

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

namespace KbHook
{
public static class Program
{
const int HC_ACTION = 0;
const int WH_KEYBOARD_LL = 13;
const int WM_KEYDOWN = 0x0100;
static IntPtr HookHandle = IntPtr.Zero;
static Form1 Form1;

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr SetWindowsHookEx(int idHook, KbHook lpfn, IntPtr hMod, uint dwThreadId);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool UnhookWindowsHookEx(IntPtr hhk);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr GetModuleHandle(string lpModuleName);

[STAThread]
static void Main()
{
try
{
using (var proc = Process.GetCurrentProcess())
using (var curModule = proc.MainModule)
{
var moduleHandle = GetModuleHandle(curModule.ModuleName);
HookHandle = SetWindowsHookEx(WH_KEYBOARD_LL, IgnoreWin_D, moduleHandle, 0);
}

Form1 = new Form1();
Application.Run(Form1);
}
finally
{
UnhookWindowsHookEx(HookHandle);
}
}

[DllImport("user32.dll")]
static extern short GetAsyncKeyState(Keys vKey);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

static IntPtr IgnoreWin_D(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode == HC_ACTION
&& IsWin_D(wParam, lParam))
return (IntPtr) 1; //just ignore the key press

return CallNextHookEx(HookHandle, nCode, wParam, lParam);
}

static bool IsWin_D(IntPtr wParam, IntPtr lParam)
{
if ((int) wParam != WM_KEYDOWN)
return false;

var keyInfo = (KbHookParam) Marshal.PtrToStructure(lParam, typeof (KbHookParam));
if (keyInfo.VkCode != (int) Keys.D) return false;
return GetAsyncKeyState(Keys.LWin) < 0
|| GetAsyncKeyState(Keys.RWin) < 0;
}

delegate IntPtr KbHook(int nCode, IntPtr wParam, [In] IntPtr lParam);

[StructLayout(LayoutKind.Sequential)]
struct KbHookParam
{
public readonly int VkCode;
public readonly int ScanCode;
public readonly int Flags;
public readonly int Time;
public readonly IntPtr Extra;
}
}
}

另见 How to hook Win + Tab using LowLevelKeyboardHook

关于c# - 在 Winforms 中捕获 WinKey+D 键序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31771464/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com