如何将密钥发送到其他应用程序以便他们按下? 我想要像
这样的东西 keystate(keys.A) = down
(按住按钮)
和
keystate(keys.A) = up
(释放按钮)
答案 0 :(得分:2)
如果要连续发送多个密钥,请使用SendKeys.Send
https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send(v=vs.110).aspx
如果您想按住键,则需要导入User32库调用:
Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
您还需要MapVirtualKey。这会将物理板(面向驱动程序)上的键布局转移到硬件不变的虚拟键集(面向软件)。
<DllImport("User32.dll", SetLastError:=False, CallingConvention:=CallingConvention.StdCall, _
CharSet:=CharSet.Auto)> _
Public Shared Function MapVirtualKey(ByVal uCode As UInt32, ByVal uMapType As MapVirtualKeyMapTypes) As UInt32
End Function
然后就这样做:
Private Sub HoldKeyDown(ByVal key As Byte, ByVal durationInSeconds As Integer)
Dim targetTime As DateTime = DateTime.Now().AddSeconds(durationInSeconds)
keybd_event(key, MapVirtualKey(key, 0), 0, 0) ' Down
While targetTime.Subtract(DateTime.Now()).TotalSeconds > 0
Application.DoEvents()
End While
keybd_event(key, MapVirtualKey(key, 0), 2, 0) ' Up
End Sub
答案 1 :(得分:0)
这是一个简单的表单,它将根据键盘上按下的左/右/上/下键在表单周围移动Car。我已将游戏更新循环设置为每秒30帧,但您可以使用不同的Thread.Sleep()更改此值:
我的表单上有一个名为&#34; lblCar&#34;的标签,我在每个游戏更新循环中都移动了位置。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(GameUpdate));
}
private bool leftPressed;
private bool rightPressed;
private bool upPressed;
private bool downPressed;
private void GameUpdate(object state)
{
bool gameRunning = true;
do
{
if (leftPressed)
{
BeginInvoke(new Action(() => { lblCar.Location = new Point(lblCar.Location.X - 1, lblCar.Location.Y); }));
}
if (rightPressed)
{
BeginInvoke(new Action(() => { lblCar.Location = new Point(lblCar.Location.X + 1, lblCar.Location.Y); }));
}
if (upPressed)
{
BeginInvoke(new Action(() => { lblCar.Location = new Point(lblCar.Location.X, lblCar.Location.Y - 1); }));
}
if (downPressed)
{
BeginInvoke(new Action(() => { lblCar.Location = new Point(lblCar.Location.X, lblCar.Location.Y + 1); }));
}
Thread.Sleep(33); // 30 frames per second
} while (gameRunning);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Left: leftPressed = true; break;
case Keys.Right: rightPressed = true; break;
case Keys.Up: upPressed = true; break;
case Keys.Down: downPressed = true; break;
default: break;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Left: leftPressed = false; break;
case Keys.Right: rightPressed = false; break;
case Keys.Up: upPressed = false; break;
case Keys.Down: downPressed = false; break;
default: break;
}
}
}
答案 2 :(得分:0)
(visul studio)代码是
Imports System.Runtime.InteropServices
Public Class Form1
Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End Sub
<DllImport("User32.dll", SetLastError:=False, CallingConvention:=CallingConvention.StdCall, _
CharSet:=CharSet.Auto)> _
Public Shared Function MapVirtualKey(ByVal uCode As UInt32, ByVal uMapType As UInt32) As UInt32
End Function
Private Sub HoldKeyDown(ByVal key As Byte, ByVal durationInSeconds As Integer)
Dim targetTime As DateTime = DateTime.Now().AddSeconds(durationInSeconds)
keybd_event(key, MapVirtualKey(key, 0), 0, 0) ' Down
While targetTime.Subtract(DateTime.Now()).TotalSeconds > 0
Application.DoEvents()
End While
keybd_event(key, MapVirtualKey(key, 0), 2, 0) ' Up
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
HoldKeyDown(Keys.A, 5)
End Sub
End Class