Rah*_*rma 1 .net c# vb.net wpf winforms
我制作了一个带有文本框的 Windows 窗体应用程序,它使用条形码扫描仪来获取任何输入值。我希望用户仅使用条形码扫描仪来填充其中的任何值,并且不想使用我的常规键盘输入任何输入。
由于我的条形码的工作方式类似于键盘,因此禁用我的常规键盘也会禁用我的条形码扫描仪的工作。
我搜索了很多地方来实现这一点,发现很少有答案建议添加秒表/计时器来消除 50 毫秒内发生的所有按键,因为条形码可以在 50 毫秒内扫描所有值,但没有人可以比 50 毫秒更快地打字。
我也尝试过这种方法,但是当我随机用手指敲击键盘按键时,它会失败,因为某些按键在 50 毫秒内触发,所以它会读出。
还尝试了下面的代码,但即使这对我来说也不能按预期工作
private void rtBoxInput_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true;
}
Run Code Online (Sandbox Code Playgroud)
请建议一些好方法来实现这个?
小智 5
基本思想是检查:
如果 KeyUp 和 KeyDown 事件是由相同的按键在指定的时间(例如 17 毫秒)内触发的,因为这只能使用条形码扫描仪来完成。
17毫秒内没有人可以触发同一按键的KeyDown和KeyUp事件。例如,某人按下并释放同一个键需要花费超过指定的时间,但是他可以敲打键盘,将多个键一起按下并触发其 KeyDown 和 KeyUp 事件,但所有键都不会发生 KeyUp 和 KeyDown 事件同步发射。这样你就可以检测输入是通过普通键盘还是条码扫描仪进行的。
请看下面:
public partial class BarcodeReader : Form
{
char cforKeyDown = '\0';
int _lastKeystroke = DateTime.Now.Millisecond;
List<char> _barcode = new List<char>(1);
bool UseKeyboard = false;
public BarcodeReader()
{
InitializeComponent();
}
private void BarcodeReader_Load(object sender, EventArgs e)
{
this.KeyDown += new KeyEventHandler(BarcodeReader_KeyDown);
this.KeyUp += new KeyEventHandler(BarcodeReader_KeyUp);
}
private void BarcodeReader_KeyUp(object sender, KeyEventArgs e)
{
// if keyboard input is allowed to read
if (UseKeyboard && e.KeyData != Keys.Enter)
{
MessageBox.Show(e.KeyData.ToString());
}
/* check if keydown and keyup is not different
* and keydown event is not fired again before the keyup event fired for the same key
* and keydown is not null
* Barcode never fired keydown event more than 1 time before the same key fired keyup event
* Barcode generally finishes all events (like keydown > keypress > keyup) of single key at a time, if two different keys are pressed then it is with keyboard
*/
if (cforKeyDown != (char)e.KeyCode || cforKeyDown == '\0')
{
cforKeyDown = '\0';
_barcode.Clear();
return;
}
// getting the time difference between 2 keys
int elapsed = (DateTime.Now.Millisecond - _lastKeystroke);
/*
* Barcode scanner usually takes less than 17 milliseconds as per my Barcode reader to read , increase this if neccessary of your barcode scanner is slower
* also assuming human can not type faster than 17 milliseconds
*/
if (elapsed > 17)
_barcode.Clear();
// Do not push in array if Enter/Return is pressed, since it is not any Character that need to be read
if (e.KeyCode != Keys.Return)
{
_barcode.Add((char)e.KeyData);
}
// Barcode scanner hits Enter/Return after reading barcode
if (e.KeyCode == Keys.Return && _barcode.Count > 0)
{
string BarCodeData = new String(_barcode.ToArray());
if (!UseKeyboard)
MessageBox.Show(String.Format("{0}", BarCodeData));
_barcode.Clear();
}
// update the last key press strock time
_lastKeystroke = DateTime.Now.Millisecond;
}
private void BarcodeReader_KeyDown(object sender, KeyEventArgs e)
{
//Debug.WriteLine("BarcodeReader_KeyDown : " + (char)e.KeyCode);
cforKeyDown = (char)e.KeyCode;
}
}
Run Code Online (Sandbox Code Playgroud)
检查这里.. GitHub 链接
| 归档时间: |
|
| 查看次数: |
17198 次 |
| 最近记录: |