0

在 KeyDown 事件中,我曾经SuppressKeyPress避免调用KeyPressKeyUp事件。但是,尽管KeyPress事件已停止,但KeyUp事件仍会触发。为什么是这样?

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.H)
    {
        listBox1.Items.Add("key down" + e.KeyCode);
        // e.SuppressKeyPress = true;
    }
}

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == 'h')
    {
        listBox1.Items.Add("key press" + e.KeyChar);
    }
}

private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
    if(e.KeyCode==Keys.H)
    {
        listBox1.Items.Add("key up" + e.KeyCode);
    }
}
4

2 回答 2

1

看看 SuppressHeyPress 在 Control 类中是如何处理的:

protected virtual bool ProcessKeyEventArgs(ref Message m)
{
    // ...
    if (e.SuppressKeyPress)
    {
        this.RemovePendingMessages(0x102, 0x102);
        this.RemovePendingMessages(0x106, 0x106);
        this.RemovePendingMessages(0x286, 0x286);
    }
    return e.Handled;
}

很明显,您不能做这样的事情来抑制 WM_KEYUP 消息(当您处理 KeyDown 事件时,KeyPress 消息已经发送到您的控件,但 KeyUp 消息在用户释放键之前不会触发)。

您可以使用以下代码对此进行测试:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool PeekMessage([In, Out] ref MSG msg, HandleRef hwnd, int msgMin, int msgMax, int remove);

[Serializable, StructLayout(LayoutKind.Sequential)]
public struct MSG
{
    public IntPtr hwnd;
    public int message;
    public IntPtr wParam;
    public IntPtr lParam;
    public int time;
    public int pt_x;
    public int pt_y;
}

private void RemovePendingMessages(Control c, int msgMin, int msgMax)
{
    if (!this.IsDisposed)
    {
        MSG msg = new MSG();
        IntPtr handle = c.Handle;
        while (PeekMessage(ref msg, new HandleRef(c, handle), msgMin, msgMax, 1))
        {
        }
    }
}

private void SuppressKeyPress(Control c)
{
    this.RemovePendingMessages(c, 0x102, 0x102);
    this.RemovePendingMessages(c, 0x106, 0x106);
    this.RemovePendingMessages(c, 0x286, 0x286);
}

private void SuppressKeyUp(Control c)
{
    this.RemovePendingMessages(c, 0x101, 0x101);
    this.RemovePendingMessages(c, 0x105, 0x105);
}

private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.H)
    {
        SuppressKeyPress(sender);       // will work
        SuppressKeyUp(sender);          // won't work
    }
}

一个解决方案是使用布尔标志 suppressKeyUp,在 KeyDown 将其设置为 true 并检查它并在 KeyUp 中将其重置,但您必须彻底检查它并查看当用户行为不端时会发生什么(例如按下两个键)。

于 2012-05-29T11:20:58.820 回答
0

是的,试着放

e.Handled = true;

之后e.Suppress... = true;

于 2012-05-29T08:59:21.023 回答