gpt4 book ai didi

c# - SendKey.Send() 不工作

转载 作者:太空狗 更新时间:2023-10-29 20:01:36 24 4
gpt4 key购买 nike

我正在使用 WPF 并且导入了 System.Windows.Form 引用。这是我的代码:

Process[] process = Process.GetProcessesByName("wmplayer");
SetForegroundWindow(process[0].MainWindowHandle);
Thread.Sleep(200);
System.Windows.Forms.SendKeys.Send("^p");

Windows Media Player 聚焦,但未收到击键。为什么?

最佳答案

您可以使用 WinAPI 而不是 SendKeys:

[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
public static void PressKey(Keys key, bool up) {
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
if (up) {
keybd_event((byte) key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr) 0);
}
else {
keybd_event((byte) key, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr) 0);
}
}

void TestProc() {
PressKey(Keys.ControlKey, false);
PressKey(Keys.P, false);
PressKey(Keys.P, true);
PressKey(Keys.ControlKey, true);
}

关于c# - SendKey.Send() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11402643/

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