我正在尝试从代码后面发送复合击键。我的意思是发送像Ctrl + Tab这样的东西。我尝试使用KeyEventArgs如下:
KeyEventArgs args1 = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, Key.LeftCtrl);
KeyEventArgs args2 = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, Key.Tab);
args1.RoutedEvent = Keyboard.KeyDownEvent;
args2.RoutedEvent = Keyboard.KeyDownEvent;
InputManager.Current.ProcessInput(args1);
InputManager.Current.ProcessInput(args2);
但是,这不起作用。有谁知道我怎么能这样做?
答案 0 :(得分:1)
你可以这样做......
var modKey = ModifierKeys.Control;
var device = new MYKeyboardDevice(InputManager.Current)
{
ModifierKeysImpl = modKey
};
var keyEventArgs = device.CreateKeyEventArgs(Key.Tab, modKey);
<强> MYKeyboardDevice 强>
public sealed class MYKeyboardDevice : KeyboardDevice
{
private sealed class MYPresentationSource : PresentationSource
{
Visual _rootVisual;
protected override CompositionTarget GetCompositionTargetCore()
{
throw new NotImplementedException();
}
public override bool IsDisposed
{
get { return false; }
}
public override Visual RootVisual
{
get { return _rootVisual; }
set { _rootVisual = value; }
}
}
private static RoutedEvent s_testEvent = EventManager.RegisterRoutedEvent(
"Key Event",
RoutingStrategy.Bubble,
typeof(MYKeyboardDevice),
typeof(MYKeyboardDevice));
public ModifierKeys ModifierKeysImpl;
public MYKeyboardDevice()
: this(InputManager.Current)
{
}
public MYKeyboardDevice(InputManager manager)
: base(manager)
{
}
protected override KeyStates GetKeyStatesFromSystem(Key key)
{
var hasMod = false;
switch (key)
{
case Key.LeftAlt:
case Key.RightAlt:
hasMod = HasModifierKey(ModifierKeys.Alt);
break;
case Key.LeftCtrl:
case Key.RightCtrl:
hasMod = HasModifierKey(ModifierKeys.Control);
break;
case Key.LeftShift:
case Key.RightShift:
hasMod = HasModifierKey(ModifierKeys.Shift);
break;
}
return hasMod ? KeyStates.Down : KeyStates.None;
}
public KeyEventArgs CreateKeyEventArgs(
Key key,
ModifierKeys modKeys = ModifierKeys.None)
{
var arg = new KeyEventArgs(
this,
new MYPresentationSource(),
0,
key);
ModifierKeysImpl = modKeys;
arg.RoutedEvent = s_testEvent;
return arg;
}
private bool RaiseEvents(UIElement target, RoutedEventArgs e, params RoutedEvent[] routedEventArray)
{
foreach (var routedEvent in routedEventArray)
{
e.RoutedEvent = routedEvent;
target.RaiseEvent(e);
if (e.Handled)
{
return true;
}
}
return false;
}
private bool HasModifierKey(ModifierKeys modKey)
{
return 0 != (ModifierKeysImpl & modKey);
}
}
答案 1 :(得分:0)
SendKeys.SendWait(“^ {Tab}”)怎么样?
添加对System.Windows.Forms的引用
将此作为其他按键的参考:http://www.autohotkey.com/docs/commands/Send.htm
答案 2 :(得分:0)
Here
是InputManager.InputProcess
支持的密钥列表,可以模拟:
None : True
Cancel : False
Back : True
Tab : True <-------
... [Skipped] ...
LeftShift : False
RightShift : False
LeftCtrl : False <-------
RightCtrl : False
我试过你的例子, Tab 对我来说工作得很好,但不是用 LeftCtrl 。在这种情况下,我认为有必要寻找替代方案。此外,据我所知,通过这种方法(也许作为其他标准方法)将无法组合按下多个键。
作为选项之一:
WPF Sendkeys project
- Information about project
此项目为WPF UIElements提供SendKeys
支持(用于进程内UI测试)。
答案 3 :(得分:0)
如何对“KeyUp”事件做出反应,保存第一个键,第二次按键时检查组合,是否符合您的要求之一?
这样做很简单..