【发布时间】:2015-06-13 14:30:14
【问题描述】:
我希望在用户不按任何键的情况下触发按键事件。 所以我用
InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_U);
还有
System.Windows.Forms.SendKeys.Send("a");
那不是原因 所以我用
var key = Key.Insert; // Key to send
var target = Keyboard.FocusedElement; // Target element
var routedEvent = Keyboard.KeyDownEvent; // Event to send
target.RaiseEvent(
new KeyEventArgs(
Keyboard.PrimaryDevice,
PresentationSource.FromVisual(target),
0,
key) { RoutedEvent = routedEvent }
);
导致以下错误 当前上下文中不存在名称“Key” 我用
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Globalization;
using System.Windows.Input;
using WindowsInput;
我在Form1_Load
上写下这段代码【问题讨论】:
-
"我在 Form1_Load 上编写此代码"
Load()事件发生在表单显示之前,因此您的击键被发送到任何应用程序在程序运行之前聚焦(如果您从 IDE 运行,可能是 Visual Studio)。将您的测试代码移至Shown()事件,您可能会得到更好的结果... -
@Idle_Mind 谢谢。它有效。
标签: c# winforms events keypress keydown