我制作了一个带有文本框的Windows窗体应用程序,该文本框使用条形码扫描程序来获取任何输入值。我希望用户只使用条形码扫描器来填充其中的任何值,并且不想使用常规键盘输入任何输入。
由于我的条形码模仿键盘,因此禁用我的常规键盘也会禁用我的条形码扫描器。
我已经在很多地方进行了搜索以实现这一点,并且发现很少有答案建议添加秒表/计时器来消除50毫秒内发生的所有按键,因为条形码可以在50毫秒内扫描所有值,但没有人可以输入快于50毫秒。
我也尝试过这种方式,但是当我在键盘上随意地敲击手指时,它会失败,因为有些键在50毫秒内被触发,所以它会读出来。
还尝试了以下代码,但即使这对我来说也无法正常工作
private void rtBoxInput_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true;
}
请提出一些实施此方法的好方法?
答案 0 :(得分:2)
基本想法是检查:
如果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;
}
}
点击此处.. GitHub Link
答案 1 :(得分:0)
如果您的条形码模仿键盘 - 您无法找到哪一个在TextBox中输入文本。您的条码扫描器可以为扫描的代码添加一些前缀吗?如果是 - 我认为这是与50ms计时器结合使用的最佳选择。
答案 2 :(得分:0)
您可以使用KeyPress事件直接在表单上处理条形码并禁用TextBox:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
barcode = string.Empty;
try
{
barcode += e.KeyChar;
if (lastTime > new DateTime())
{
if (DateTime.Now.Subtract(lastTime).Milliseconds > 30)
{
f1 = false;
}
else
{
f1 = true;
}
}
lastTime = DateTime.Now;
/*
Test your Barcode, and if it matches your criteria then change your TextBox text
TextBox1.Text = barcode;
*/
}
catch (Exception ex)
{
MessageBox.Show("Something went wrong");
}
}
不要忘记设置Form1.KeyPreview = true,它应该可以解决问题!