none
如何获得本程序内按键键值(Smartphone) RRS feed

  • 问题





  • private void Form1_KeyDown(object sender, KeyEventArgs e) { if ((e.KeyCode == System.Windows.Forms.Keys.F1)) { // Soft Key 1 // Not handled when menu is present. } if ((e.KeyCode == System.Windows.Forms.Keys.F2)) { // Soft Key 2 // Not handled when menu is present. } if ((e.KeyCode == System.Windows.Forms.Keys.Up)) { // Up } if ((e.KeyCode == System.Windows.Forms.Keys.Down)) { // Down } if ((e.KeyCode == System.Windows.Forms.Keys.Left)) { // Left } if ((e.KeyCode == System.Windows.Forms.Keys.Right)) { // Right } if ((e.KeyCode == System.Windows.Forms.Keys.Enter)) { // Enter } if ((e.KeyCode == System.Windows.Forms.Keys.D1)) { // 1 } if ((e.KeyCode == System.Windows.Forms.Keys.D2)) { // 2 } if ((e.KeyCode == System.Windows.Forms.Keys.D3)) { // 3 } if ((e.KeyCode == System.Windows.Forms.Keys.D4)) { // 4 } if ((e.KeyCode == System.Windows.Forms.Keys.D5)) { // 5 } if ((e.KeyCode == System.Windows.Forms.Keys.D6)) { // 6 } if ((e.KeyCode == System.Windows.Forms.Keys.D7)) { // 7 } if ((e.KeyCode == System.Windows.Forms.Keys.D8)) { // 8 } if ((e.KeyCode == System.Windows.Forms.Keys.D9)) { // 9 } if ((e.KeyCode == System.Windows.Forms.Keys.F8)) { // * } if ((e.KeyCode == System.Windows.Forms.Keys.D0)) { // 0 } if ((e.KeyCode == System.Windows.Forms.Keys.F9)) { // # } }
    以上是VS自己生成的KeyDown事件代码,可是按所有数字键、*、# 均得不到正确的KeyCode 其e.KeyCode都为:ProcessKey ,e.KeyData也都为:ProcessKey   e.KeyValue都为:229  Soft Key 1、Soft Key 2还都捕捉不到没有进入事件。请问要怎么在KeyDown事件中得到正确的键值呢。因为某种原因在KeyPress事件中得到的按键结果值对我来说没用。
    2009年8月21日 2:53

答案

  • *和8的key code都是一样的,下面我debug的内容,判断e的值分别按了8和*,我们可以看出如果是*, 那么最下面一项的值shift是true, 我们可以根据这个来判断是按了8还是*:
    e
    {KeyData = Back | ShiftKey | Space}
        base {System.EventArgs}: {KeyData = Back | ShiftKey | Space}
        Alt: false
        Control: false
        Handled: false
        KeyCode: Back | ShiftKey | Space
        KeyData: Back | ShiftKey | Space
        KeyValue: 56
        m_fHandled: false
        m_keyData: Back | ShiftKey | Space
        Modifiers: None
        Shift: false
    e
    {KeyData = Back | ShiftKey | Space | Shift}
        base {System.EventArgs}: {KeyData = Back | ShiftKey | Space | Shift}
        Alt: false
        Control: false
        Handled: false
        KeyCode: Back | ShiftKey | Space
        KeyData: Back | ShiftKey | Space | Shift
        KeyValue: 56
        m_fHandled: false
        m_keyData: Back | ShiftKey | Space | Shift
        Modifiers: Shift
        Shift: true

    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    2009年8月26日 9:07
    版主