如何使用 C# SendKeys 以编程方式按下 Windows 键

发布于 2024-08-05 10:20:15 字数 203 浏览 6 评论 0原文

基本上我想在代码中模拟用户单击 Windows 键。我知道有 SendKeys,如果我得到了按键的句柄,它允许我将按键发送到窗口,但我不知道我需要获取什么句柄才能发送 Windows 按键命令。例如 Windows 键 + L。读过一点后,似乎 CTRL-ESC 也应该弹出“开始”菜单,但不确定如何告诉它将键发送到 Windows(如果可能的话)。任何帮助将不胜感激。

干杯!

Basically I want to simulate in code a user clicking on the windows key. I know there is SendKeys which allows me to send key presses to windows if I get a handle to them, but what I can't figure out is what I need to get a handle on in order to send Windows key commands. E.g. Windows key + L. Having read into this a bit it appears that CTRL-ESC should pop up the Start Menu also but not sure how to tell it to send the keys to Windows (if this is even possible). Any help would be much appreciated.

Cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

初相遇 2024-08-12 10:20:15

我不认为你可以使用 SendKeys 来做到这一点,你需要 p/invoke 一个 API 函数,可能 keybd_event 用于发送 CTRL+ESC 或 Windows 键。

这里是在 VB 中以这种方式打开开始菜单的示例,这里是 pinvoke.net 上的 keybd_event 及其 C# 签名。

I don't think you can do this using SendKeys, you will need to p/invoke to an API function instead, probably keybd_event to send either CTRL+ESC or the Windows key.

Here is an example of opening the start menu this way in VB and here is keybd_event with its C# signature on pinvoke.net.

帅气称霸 2024-08-12 10:20:15

用户通过 WinKey 快捷方式执行的一些操作可以通过其他方式以编程方式完成。以 WinKey+L 为例,您可以只使用以下语句:

Process.Start("rundll32.exe", "user32.dll,LockWorkStation");

如果您可以详细说明您到底想要完成什么,也许有比 keybd_event 更好的方法(正如 Dale 所建议的那样)。

Some of the things that a user would do via a WinKey shortcut can be done programmatically in other ways. To take your WinKey+L example, you could instead just use the following statement:

Process.Start("rundll32.exe", "user32.dll,LockWorkStation");

If you could elaborate on what exactly you're trying to accomplish, maybe there's a better way than keybd_event (as Dale has suggested).

不及他 2024-08-12 10:20:15

我使用了此处user703016 并且工作正常!

参考:

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

static class KeyboardSend
{
    [DllImport("user32.dll")]
    private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

    private const int KEYEVENTF_EXTENDEDKEY = 1;
    private const int KEYEVENTF_KEYUP = 2;

    public static void KeyDown(Keys vKey)
    {
        keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY, 0);
    }

    public static void KeyUp(Keys vKey)
    {
        keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
    }
}

以这种方式使用:

KeyboardSend.KeyDown(Keys.LWin);
KeyboardSend.KeyDown(Keys.D4);
KeyboardSend.KeyUp(Keys.LWin);
KeyboardSend.KeyUp(Keys.D4);

I've used the class provided here by user703016 and worked fine!

for ref:

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

static class KeyboardSend
{
    [DllImport("user32.dll")]
    private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

    private const int KEYEVENTF_EXTENDEDKEY = 1;
    private const int KEYEVENTF_KEYUP = 2;

    public static void KeyDown(Keys vKey)
    {
        keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY, 0);
    }

    public static void KeyUp(Keys vKey)
    {
        keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
    }
}

used in this way:

KeyboardSend.KeyDown(Keys.LWin);
KeyboardSend.KeyDown(Keys.D4);
KeyboardSend.KeyUp(Keys.LWin);
KeyboardSend.KeyUp(Keys.D4);
べ映画 2024-08-12 10:20:15

您需要使用全局键盘挂钩来挂钩应用程序外部的键盘。 此处有一篇关于如何执行此操作的文章。

You need to use a global keyboard hook to hook into keyboards outside your application. There's an article on how to do it here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文