我如何计算C#中后台的按键次数
How can i count the number of keypress in the background in C#
我正在使用 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;
}
}
}
如果有人能帮助我修复这段代码,我将非常高兴。非常感谢!
要计算背景上的按键次数,您需要键盘钩子。看看这些 link:
https://www.codeproject.com/Articles/19004/A-Simple-C-Global-Low-Level-Keyboard-Hook
C# : Keyboard Hook
试试这个 Global Mouse and Keyboard Library
我相信它包含了您需要的一切。
我正在使用 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;
}
}
}
如果有人能帮助我修复这段代码,我将非常高兴。非常感谢!
要计算背景上的按键次数,您需要键盘钩子。看看这些 link:
https://www.codeproject.com/Articles/19004/A-Simple-C-Global-Low-Level-Keyboard-Hook
C# : Keyboard Hook
试试这个 Global Mouse and Keyboard Library
我相信它包含了您需要的一切。