您好我正在尝试向流程程序发送按键以使用热键更改场景,而我无法弄清楚它为什么不起作用。我尝试了几种不同的方法:sendkeys,keybd_event和postmessage,但我根本无法使用它。它还可以找到矩形的大小等,但我认为这与我的问题无关,因为这一切看起来都很好。
找到正确的虚拟密钥代码似乎没有问题,所以我不知道该怎么做。我几乎到处搜索了!我承认我不是一个C#程序员,但我通常可以在没有太多麻烦的情况下绊倒这些东西。任何帮助将不胜感激:D
编辑:分别为keydown和keyup将标志更改为0和2。
编辑:第一次按键似乎在我注释掉switchtothiswindow时工作,但随后的按键仅在alt-tabbed out of game时才有效。所以我不知道该怎么做。
public static extern void keybd_event(byte vk, byte scan, int flags, int extrainfo);
[DllImport("user32.dll")]
public static extern uint MapVirtualKey(uint uCode, uint uMapType);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
public const ushort WM_KEYDOWN = 0x0000;
public const ushort WM_KEYUP = 0x0002;
//virtual key code enum
public enum KeyCodes : byte
{
F1 = 0x70,
F2 = 0x71,
F3 = 0x72,
F4 = 0x73,
F5 = 0x74,
F6 = 0x75,
F7 = 0x76,
F8 = 0x77,
F9 = 0x78,
F10 = 0x79,
F11 = 0x7A,
F12 = 0x7B,
f1 = 0x70,
f2 = 0x71,
f3 = 0x72,
f4 = 0x73,
f5 = 0x74,
f6 = 0x75,
f7 = 0x76,
f8 = 0x77,
f9 = 0x78,
f10 = 0x79,
f11 = 0x7A,
f12 = 0x7B,
}
//enum parse
public byte FindKeyCode (string fkey)
{
return (byte)Enum.Parse(typeof(KeyCodes), fkey);
}
//press the keys
public static void PressKeys(byte key)
{
SwitchToThisWindow(obsHandle, true);
keybd_event(key, Convert.ToByte(MapVirtualKey(key, 0)), WM_KEYDOWN, 0);
keybd_event(key, Convert.ToByte(MapVirtualKey(key, 0)), WM_KEYUP, 0);
SwitchToThisWindow(wHandle, true);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
Scene1Key = FindKeyCode(textBox1.Text);
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
Scene2Key = FindKeyCode(textBox2.Text);
}