C # learning notes-KeyDown, KeyPress, KeyUp events, and KeyCode, KeyData, KeyValue, and KeyChar attributes,
I didn't plan to write it separately, but when I tested the mini-program myself, the more I wrote and found that I was not clear about it, the more I learned from it. So my practice once again proved that I had to think about it on paper, I am sure you want to do this!
Directly paste the Code:
1 // order of occurrence: keyDown-> KeyPress-> KeyUp 2 // all the parameter information is included in Keys => View by yourself 3 4 // KeyCode, KeyData, KeyValue only records uppercase values for the Keys value 5 // No KeyChar 6 // you must set the KeyPreview attribute to true 7 8 private void Form1_KeyDown (object sender, keyEventArgs e) 9 {10 if (e. alt & e. control & e. keyCode = Keys. f2) 11 {12 MessageBox. show ("You press the Alt and Ctrl and F2 buttons! "); 13} 14 if (e. KeyData = Keys. Up) 15 {16 MessageBox. Show (" You press the Up buttons! "); 17} 18 if (e. KeyValue = 27) 19 {20 MessageBox. Show (" You press the Esc buttons! "); 21} 22 23} 24 25 // only one ASCII code 26 can be returned // function keys, edit keys, and key combinations cannot be processed 27 // KeyChar can be case sensitive 28 // KeyChar characters exist in KeyPress 29 private void Form1_KeyPress (object sender, keyPressEventArgs e) 30 {31 if (e. keyChar = 65) 32 {33 MessageBox. show ("You press the buttons! "); 34} 35 if (e. KeyChar = 97) 36 {37 MessageBox. Show (" You press the buttons! "); 38} 39 // KeyChar is the 40 if (e. keyChar = 48) 41 {42 MessageBox. show ("You press the 0 buttons! "); 43} 44} 45 46 47 // No KeyChar48 private void Form1_KeyUp (object sender, KeyEventArgs e) 49 {50 // similar to KeyDown 51 // The keypad number 052 if (e. keyValue = 96) 53 {54 MessageBox. show ("You press the 0 buttons in keypad! "); 55} 56 // keypad number 057 if (e. KeyCode = Keys. NumPad0) 58 {59 MessageBox. Show (" You press the 0 buttons in keypad! "); 60} 61 // The number 062 on the primary keyboard if (e. keyCode = Keys. d0) 63 {64 MessageBox. show ("You press the 0 buttons in primary keyboard! "); 65} 66 67}
In addition, I wish you a happy National Day! I am also going to attend my brother's wedding together .......