SendKeys.Send和Windows键SendKeys、Send、Windows

2023-09-02 10:40:26 作者:欲望撕扯人性i

我试图做一个键盘触发我调用了Windows键+#(#=数字键)快捷键这将启动任务栏上的第n个插槽的应用程序的新实例WinForm应用程序。

I'm trying to make a keyboard trigger for my WinForm application that calls out Windows key + # (# = a number key) shortcut which launches a new instance of the application in the nth slot on the taskbar.

例如,我想运行Photoshop,这是我的任务栏的第一个插槽。

For example, I would like to run Photoshop, which is in the first slot of my taskbar.

WinKey+1

我听说CTRL + ESC替代的,但code以下无法正常工作。

I've heard of the CTRL+ESC alternative, but the code below does not work.

SendKeys.Send("^{ESC}1")

任何其他的选择吗?我是否需要运行这个批处理文件?

Any other alternatives? Do I need to run a batch file for this?

谢谢! :)

推荐答案

CTRL + ESC 不模拟 WIN 键,它只是调用start菜单。

CTRL+ESC does not simulate the WIN key, it just calls the start menu.

P的位/调用总是让每个人都高兴:

A bit of P/Invoke always makes everyone happy:

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);

经测试,它的工作原理。玩得开心!

Tested, it works. Have fun!

 
精彩推荐
图片推荐