读取条码扫描器条目时专注于按钮

问题描述:

我使用条码扫描器监听使用按键事件如下(从另一个岗位):读取条码扫描器条目时专注于按钮

public Form2() { 
    InitializeComponent(); 
    // 
    this.KeyPreview = true; 
    this.KeyPress += Form2_KeyPress; 
    this.Button1_click += (s, e) => { 
      // --- even if I don't close the form, the click event firing 
      // prevents the "Process barcode" to execute... 
      //this.Close(); 
      Console.Writeln("hitting focused button."); 
     } 
} 
public void KeyPress_scanner_preview(object sender, KeyPressEventArgs e) { 
     // check timing (keystrokes within 100 ms) 
     TimeSpan elapsed = (DateTime.Now - _lastKeystroke); 
     if (elapsed.TotalMilliseconds > 100) 
     _barcode.Clear(); 

     // record keystroke & timestamp 
     _barcode.Add(e.KeyChar); 
     _lastKeystroke = DateTime.Now; 

     // process barcode 
     if (e.KeyChar == 13 && _barcode.Count > 0) { 
     string msg = new String(_barcode.ToArray()); 
     MessageBox.Show("Read barcode: " + new String(_barcode.ToArray())); 
     _barcode.Clear(); 
     } 
    } 

现在我的问题是,随着我的扫描仪,当我专注于一个按钮,“button_click”事件触发之前BarCodeScanned被解雇。

任何提示如何防止这种情况发生?可能禁用按钮?

编辑:我添加了按钮单击事件处理程序和窗体的构造函数。请注意,我自己有一个按钮,因此自动对焦。点击“按钮点击”事件可以防止条形码事件被触发(这里显示一个消息框)。请注意,我是否注册按钮单击事件没有任何区别...

我只会重新TaW's答案,以反映打“回车”应该不火的空条码扫描事件的情况。

注意与原始参数,我能够很容易地从keayboard发射条码扫描事件。因此,我将缩短的打字时间缩短到50ms,并确保_barcode至少有7个字符长。这与EAN-8和EAN-13格式相符。即使使用双手,我也无法错误地触发条码受限事件。

UPDATE逻辑错误,因为ProcessCmdKey应返回true如果处理事件密钥。我在更新的代码中实现了逻辑。

下面的代码:

DateTime _lastKeystroke = new DateTime(0); 
string _barcode = string.Empty; 

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
{ 
    bool res = processKey(keyData); 
    return keyData == Keys.Enter ? res : base.ProcessCmdKey(ref msg, keyData); 
} 

bool processKey(Keys key) 
{ 
    // check timing (>7 keystrokes within 50 ms) 
    TimeSpan elapsed = (DateTime.Now - _lastKeystroke); 
    if (elapsed.TotalMilliseconds > 50) 
    { 
     _barcode = string.Emtpy; 
    } 
    // record keystroke & timestamp 
    _barcode += (char)key; 
    _lastKeystroke = DateTime.Now; 
    // process barcode 
    // --- barcode length should be > 1, not 0 (as the return char 
    // itself was added above) 
    if (key == Keys.Enter && _barcode.Length > 1) 
    { 
     string msg = new String(_barcode.ToArray()); 
     MessageBox.Show(msg); 
     _barcode = string.Empty; 
     return true; 
    } 
    return false; 
} 

您可以在必要时将焦点设置回目标。

你没有表现出足够的代码覆盖所有的基地,但是这应该给你的想法:

private void button1_Click(object sender, EventArgs e) 
{ 
    // do the real click work..: 

    // then reset the focus 
    resetFocus(); 
} 

void resetFocus() // rest focus to a textbox and clear the selection 
{ 
    textBox1.Focus(); 
    textBox1.SelectionLength = 0; 
    textBox1.SelectionStart = textBox1.Text.Length; 
} 

更新1:

如果您Button没有收到焦点,因为它是,但因为没有别的东西可以获得焦点您可以通过将PreviewKeyPress事件添加到表格但解决方法但是吨。设置Form.KeyPreview=true

更新2: 如果你有许多按钮和其他的东西,你仍然可以通过重写ProcessCmdKey解决方法。仅在表单上使用PreviewKeyDown的常规方式将不起作用Buttons在传递之前窃取Enter密钥。无需立即设置Form.KeyPreview

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
{ 
    if (keyData == Keys.Enter) 
    { 
     processKey((char) keyData); // we need the Enter key 
     return false;    // do not steal it! 
    } 
    processKey((char)keyData);  // process other keys 
    return base.ProcessCmdKey(ref msg, keyData); 
} 

对于这种移动处理该按键为功能代码:

void processKey(Keys key) 
{ 

    // check timing (keystrokes within 100 ms) 
    TimeSpan elapsed = (DateTime.Now - _lastKeystroke); 
    if (elapsed.TotalMilliseconds > 100) 
     _barcode = ""; // Clear(); 

    // record keystroke & timestamp 
    _barcode += (char)key; // e.KeyChar); 
    _lastKeystroke = DateTime.Now; 

    // process barcode 

    if (key == Keys.Enter && _barcode.Length > 0) 
    { 
     string msg = new String(_barcode.ToArray()); 
     if (BarCodeScanned != null) 
     { 
      BarCodeScanned(sender, 
          new BarcodeScannedEventArgs(new String(_barcode.ToArray()))); 
     } 

     BarCodeScanned(_barcode); 
     _barcode.Clear(); 
    } 

} 
+0

我已经更新了答案;请看看是否适合你的需求更好..点检查空格键是否仍然可以按下按钮! – TaW

+0

谢谢,它的工作原理。作为一个侧面的问题:假如我不知道哪个按钮具有焦点并且我希望我的整个表单首先对条形码条目作出反应,我怎么能使这个“通用”?当我关注它们时,是否必须为所有按钮注册“preview_key”?没有其他办法吗? – neggenbe

+0

是的。我已经更新了更新。 – TaW