我想做一个应用程序,如果它匹配模式,它将替换每个输入。 例如,如果用户按下LeftMouseButton + Ctrl,程序会将其更改为右键单击,并仅将其发送到当前活动窗口或捕获窗口。
问题是如何在c#中解决它?
答案 0 :(得分:4)
您需要实现这样的类,您必须调整它以支持鼠标点击以满足您的需求,但它应该向您展示一些第一步。
public class KeyConverter {
//All conversions are stored in this dictionary.
private Dictionary<Keys, Keys> conversions = new Dictionary<Keys, Keys>();
public KeyConverter() {
//this conversion will convert every Ctrl+C signal into Ctrl+V
conversions.Add(Keys.C | Keys.Control, Keys.V | Keys.Control);
}
public Keys Convert(Keys keys) {
if (conversions.ContainsKey(keys))
return conversions[keys];
else
return keys; //return the input if no conversion is available
}
}
将您需要的转化添加到conversions-Dictionary。订阅观察击键的事件和调用方法使用当前按下的键转换。 使用
将返回的密钥发送到您的系统[DllImport("user32.dll")]
public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
public void SendKey(Keys keys){
foreach(Keys key in Enum.GetValues(typeof(Keys)))
if(keys.HasFlag(key))
keybd_event((byte)key, 0, 0, 0); //press key
foreach(Keys key in Enum.GetValues(typeof(Keys)))
if(keys.HasFlag(key))
keybd_event((byte)key, 0, 0x2, 0); // release key
}