gpt4 book ai didi

c# - 模拟 ctrl key down 事件和 ctrl key up 事件后 Ctrl key keep down

转载 作者:太空狗 更新时间:2023-10-29 21:44:24 27 4
gpt4 key购买 nike

我有一个模拟ctr+c & ctr+v的小程序(复制粘贴)使用系统 keybd_event 的事件。问题是程序运行后计算机继续运行,就好像按下 ctrl 键然后 - 如果我键入 a 它会选择整个文档,如果我滚动鼠标滚轮会改变文本面等。它不仅发生在 Visual Studio 编辑器中,而且发生在程序以 Word 等方式运行时打开的任何其他程序中。这是我的代码:

    //The system keyboard event.
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

public const int KEYEVENTF_EXTENDEDKEY = 0x0001; //Key down flag
public const int KEYEVENTF_KEYUP = 0x0002; //Key up flag
public const int VK_LCONTROL = 0xA2; //Left Control key code
public const int C = 0x43; // C key code
public const int V = 0x56; // V key code

static void Main(string[] args)
{
Thread.Sleep(1000);// So I have time to select something.

//Simulate ctrl+c
keybd_event(VK_LCONTROL, 0, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(C, 0, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(C, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_LCONTROL, 0, KEYEVENTF_KEYUP, 0);

//Simulate ctrl+v
keybd_event(VK_LCONTROL, 0, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(V, 0, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(V, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_LCONTROL, 0, KEYEVENTF_KEYUP, 0);
}

有人知道我该怎么做才能解决这个问题吗?

最佳答案

这是解决方案,这对我来说完全有效。注意发送到 keybd_event 的参数的变化。我用了CodeProject的一篇文章,链接:http://www.codeproject.com/Articles/7305/Keyboard-Events-Simulation-using-keybd-event-funct .这是我修复后的代码:

    //The system keyboard event.
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
public const int KEYEVENTF_KEYUP = 0x0002; //Key up flag
public const int VK_CONTROL = 0x11; //Control key code
public const int C = 0x43; // C key code
public const int V = 0x56; // V key code

static void Main(string[] args)
{
Thread.Sleep(1000);// So I have time to select something.

// Simulating Ctrl+C
keybd_event(VK_CONTROL, 0x9d, 0, 0); // Ctrl Press
keybd_event(C, 0x9e, 0, 0); // ‘A’ Press
keybd_event(C, 0x9e, KEYEVENTF_KEYUP, 0); // ‘A’ Release
keybd_event(VK_CONTROL, 0x9d, KEYEVENTF_KEYUP, 0); // Ctrl Release

// Simulating Ctrl+V
keybd_event(VK_CONTROL, 0x9d, 0, 0); // Ctrl Press
keybd_event(V, 0x9e, 0, 0); // ‘A’ Press
keybd_event(V, 0x9e, KEYEVENTF_KEYUP, 0); // ‘A’ Release
keybd_event(VK_CONTROL, 0x9d, KEYEVENTF_KEYUP, 0); // Ctrl Release
}

我希望这会对某人有所帮助。感谢所有帮助过我的人!

关于c# - 模拟 ctrl key down 事件和 ctrl key up 事件后 Ctrl key keep down,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27671074/

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