【发布时间】:2018-01-27 20:39:33
【问题描述】:
我正在使用 C# 和 .NET 4 为我的朋友制作一个小程序。我是一名编码工程师。 我想计算在键盘上按 X 和 Y 的次数。我设法让它在后台运行,但计数有问题。我尝试搜索可能对我有帮助的功能,但我没有找到任何东西。 程序现在正在运行,如果按下 X 或 Y,计数器就会旋转。
这就是我所拥有的:
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.Threading;
using System.Windows.Input;
namespace gombszamlalo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool fut = true;
private void Form1_Load(object sender, EventArgs e)
{
Thread TH = new Thread(billentyuzet);
CheckForIllegalCrossThreadCalls = false;
TH.SetApartmentState(ApartmentState.STA);
TH.Start();
}
public int x = 0;
public int y = 0;
void billentyuzet()
{
while (fut)
{
if ((Keyboard.GetKeyStates(Key.X) & KeyStates.Down) > 0)
{
x++;
label4.Text = x.ToString();
}
if ((Keyboard.GetKeyStates(Key.Y) & KeyStates.Down) > 0)
{
y++;
label5.Text = y.ToString();
}
if ((Keyboard.GetKeyStates(Key.F6) & KeyStates.Down) > 0)
{
fut = false;
}
}
}
如果有人可以帮助我修复此代码,我将非常高兴。非常感谢!
【问题讨论】:
标签: c# count background keypress