Incorrect, missing, and duplicate keypress events

2019-07-18 10:38发布

问题:

There appears to be a bug in how the OS X implementation of keyboard events is handled. The bug reveals itself when either multiple keys are pressed in rapid succession or all at once.

Running my app on Win7 if I hit the keys 'J' 'K' 'L' all at once, I can always get all three keys (though not in any particular order). But on OS X, while running Windows Forms applications, I can get 'JJJ' or 'JKK' or 'LLL'. Nowhere else in OS X is this behavior exhibited (native Cocoa apps, such as TextEdit, behave identical to Win7).

Source code: (in Visual Studio, create a windows forms project, and edit Form1's code):

public partial class Form1 : Form
{
    public Form1(){
        InitializeComponent();
        KeyPress += Form1_KeyPress;
        KeyDown += Form1_KeyDown;}
    void Form1_KeyDown(object sender, KeyEventArgs e)
        {Console.WriteLine("KeyDown: " + e.KeyCode.ToString());}
    void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {Console.WriteLine("KeyPress: " + e.KeyChar.ToString());}
}

Running on Windows 7 (hitting JKL all at once):

KeyDown: L
KeyPress: l
KeyDown: J
KeyPress: j
KeyDown: K
KeyPress: k

Running on OS X (hitting JKL all at once; note it can take a couple attempts)

KeyDown: L
KeyPress: k
KeyDown: J
KeyPress: k
KeyDown: K
KeyPress: k

If you missed it, all KeyPress events are 'k', the result was 'kkk'. Why is that?

Update: I'm running Mono 2.10.10, on Mountain Lion.

标签: c# winforms mono