C# 如何将键盘和鼠标输入写入端口
我正试图找出如何将键盘和鼠标输入写入端口。在我的代码中,鼠标坐标每15毫秒写入端口C# 如何将键盘和鼠标输入写入端口,c#,C#,我正试图找出如何将键盘和鼠标输入写入端口。在我的代码中,鼠标坐标每15毫秒写入端口 private void Form1_MouseMove(object sender, MouseEventArgs e) { writeToPort(new Point(e.X, e.Y)); } public void writeToPort(Point coordinates) { if (watch.ElapsedMilliseconds > 15) { w
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
writeToPort(new Point(e.X, e.Y));
}
public void writeToPort(Point coordinates)
{
if (watch.ElapsedMilliseconds > 15)
{
watch = Stopwatch.StartNew();
port.Write(String.Format("X{0}Y{1}",
(coordinates.X / (Size.Width / 180)),
(coordinates.Y / (Size.Height / 180))));
然后我有一个名为Key的int,它根据你按下的键改变了它的值(w把它的值改为1,a把它的值改为2,s把它的值改为3,d把它的值改为4),我试着把它写到井的端口上
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
writeToPort(new Point(e.X, e.Y));
}
public void writeToPort(Point coordinates)
{
if (watch.ElapsedMilliseconds > 15)
{
watch = Stopwatch.StartNew();
port.Write(String.Format("X{0}Y{1}K{2}",
(coordinates.X / (Size.Width / 180)),
(coordinates.Y / (Size.Height / 180)),
Key));
但我发现,当鼠标静止不动时,键没有写入端口
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
writeToPort(new Point(e.X, e.Y));
}
public void writeToPort(Point coordinates)
{
if (watch.ElapsedMilliseconds > 15)
{
watch = Stopwatch.StartNew();
port.Write(String.Format("X{0}Y{1}",
(coordinates.X / (Size.Width / 180)),
(coordinates.Y / (Size.Height / 180))));
当我按下按键时,如何将按键写入端口?您可以使用全局变量并将您的坐标存储在那里,当鼠标移动时,您可以在鼠标移动时更新此坐标,并使用计时器每15毫秒写入一次此坐标 不能使用鼠标移动事件每15毫秒触发一次写入操作,它将仅在移动鼠标时触发
System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
Point lastCoords;
private void Form1_Load(object sender, System.EventArgs e){
myTimer.Tick += new EventHandler(TimerEventProcessor);
myTimer.Interval = 15;
myTimer.Start();
}
private void TimerEventProcessor(Object myObject,
EventArgs myEventArgs) {
if(lastCoords != null)
writeToPort(lastCoords);
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
lastCoords = new Point(e.X, e.Y);
//writeToPort(new Point(e.X, e.Y));
}
public void writeToPort(Point coordinates)
{
port.Write(String.Format("X{0}Y{1}K{2}",
(coordinates.X / (Size.Width / 180)),
(coordinates.Y / (Size.Height / 180)),
Key));
}
您可以使用全局变量并将坐标存储在那里,当鼠标移动时,您可以在鼠标移动时更新此坐标,并使用计时器每15毫秒写入一次此坐标 不能使用鼠标移动事件每15毫秒触发一次写入操作,它将仅在移动鼠标时触发
System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
Point lastCoords;
private void Form1_Load(object sender, System.EventArgs e){
myTimer.Tick += new EventHandler(TimerEventProcessor);
myTimer.Interval = 15;
myTimer.Start();
}
private void TimerEventProcessor(Object myObject,
EventArgs myEventArgs) {
if(lastCoords != null)
writeToPort(lastCoords);
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
lastCoords = new Point(e.X, e.Y);
//writeToPort(new Point(e.X, e.Y));
}
public void writeToPort(Point coordinates)
{
port.Write(String.Format("X{0}Y{1}K{2}",
(coordinates.X / (Size.Width / 180)),
(coordinates.Y / (Size.Height / 180)),
Key));
}
你说的“到港口”是什么意思。什么端口?与其将代码添加到
MouseMove
事件处理程序,不如尝试创建一个新的事件处理程序,并将代码放在那里。@JohnWu那么它只会在发生keydown时触发,他的问题是,他需要每15毫秒写入一次当前位置,而不考虑操作使用计时器?…这是什么意思“到港口去“。什么端口?不要将代码添加到MouseMove
事件处理程序中,而是尝试创建一个新的事件处理程序,并将代码放在那里。@JohnWu则只有在发生keydown时才会触发,他的问题是,他需要每15毫秒写入一次当前位置,而不管动作如何使用计时器?…15毫秒的间隔与Windows用于60帧/秒鼠标光标移动的16.666毫秒帧时间不一致。另外,从窗口消息而不是通过DirectInput/XInput获取光标坐标的速度会慢一些(由于窗口消息泵固有的延迟)。问题不在于鼠标指针的准确性,他想每15毫秒写入最后一个光标位置,我正在解决这个问题,我只是纠正了他的逻辑以适应这种情况,我从来没有说过使用windows消息,我要求他使用一个全局作用域变量(表单级别),并将值赋给它,而不是写入端口。如果他的鼠标点更改没有像你提到的那样在15毫秒内触发,他将得到他的最后位置。用样本更新。非常感谢你,我对编码非常陌生,一直在努力让它工作,现在它做到了。我非常感谢你的帮助。谢谢。15毫秒间隔与Windows用于60帧/秒鼠标光标移动的16.666毫秒帧时间不一致。另外,从窗口消息而不是通过DirectInput/XInput获取光标坐标的速度会慢一些(由于窗口消息泵固有的延迟)。问题不在于鼠标指针的准确性,他想每15毫秒写入最后一个光标位置,我正在解决这个问题,我只是纠正了他的逻辑以适应这种情况,我从来没有说过使用windows消息,我要求他使用一个全局作用域变量(表单级别),并将值赋给它,而不是写入端口。如果他的鼠标点更改没有像你提到的那样在15毫秒内触发,他将得到他的最后位置。用样本更新。非常感谢你,我对编码非常陌生,一直在努力让它工作,现在它做到了。我非常感谢你的帮助。非常感谢。