[英]I want to be able to hold down control by code
I'm using c#, Visual Studio 2010 for a Windows application. 我正在将c#,Visual Studio 2010用于Windows应用程序。
I want to hold down the Ctrl key, then release it after a while. 我想按住Ctrl键,然后过一会儿再释放它。
I tried this but it didnt work. 我试过了,但是没用。
[DllImport("user32.dll")]
static extern bool keybd_event(byte bVk, byte bScan, uint dwFlags,
UIntPtr dwExtraInfo);
public const uint KEYEVENTF_KEYUP = 0x02;
public const uint VK_CONTROL = 0x11;
// Press the Control key.
keybd_event(VK_CONTROL,0,0,0);
//release the control key
keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
But I get this error 但是我得到这个错误
The best overloaded method match for 'ImageR.Form1.keybd_event(byte, byte, uint, System.UIntPtr) 最佳重载方法匹配为'ImageR.Form1.keybd_event(byte,byte,uint,System.UIntPtr)
cannot convert from 'uint' to 'byte' 无法从'uint'转换为'byte'
cannot convert from 'int' to 'System.UIntPtr' 无法从“ int”转换为“ System.UIntPtr”
I didn't find any good examples of do SendInput
, the examples at http://www.pinvoke.net/default.aspx/user32.keybd_event didn't work for me. 我找不到do SendInput
任何好例子, http: do SendInput
的例子对我不起作用。
The best solution to solve this was at http://inputsimulator.codeplex.com/ 解决此问题的最佳方法是在http://inputsimulator.codeplex.com/
Now it's this simple: 现在很简单:
//Press Ctrl
InputSimulator.SimulateKeyDown(VirtualKeyCode.CONTROL);
//Release Ctrl
InputSimulator.SimulateKeyUp(VirtualKeyCode.CONTROL);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.