The keypress event in Form cannot be used. The formkeypress event

Source: Internet
Author: User

The keypress event in Form cannot be used. The formkeypress event
Keypress events in Form cannot be used

 

Prepared by: CC Dad

 

November

Recently, When you modify the system, you want to add a key combination function to the screen, but add code to the form_keypress event. However, this action cannot be triggered in any case.

Today, I want to share with you the reason for the Code not being executed. Here we will make a summary for your reference. If you are interested, you can discuss and learn it together. Otherwise, you can skip it.

 

1. First, find the root cause of my problem: KeyPreview is not set to true. If it is not set to true, if the screen has a button, set points such as return enter will definitely be on the button.

So firstSetKeyPreviewSet to true.

2. Let's take a look at how to solve the key combination problem:

1) two key combinations are easier to process

1 private void frmLocalUpdate_KeyPress (object sender, KeyPressEventArgs e) // CTRL + S
2 {
3 if (Control. ModifierKeys & Keys. Control) = Keys. Control & e. KeyChar = 19)
4 {
5 OpenUpload ();
6}
7}

Use the Code in the following table to specify the characters that are not displayed on the press Key (such as Enter or Tab) and keys that represent operations rather than characters.

Key

Code

Backspace

{BACKSPACE}, {BS}, or {BKSP}

Break

{BREAK}

Caps Lock

{CAPSLOCK}

Del or Delete

{DELETE} or {DEL}

Down key

{DOWN}

End

{END}

Enter

{ENTER} Or ~

Esc

{ESC}

Help

{HELP}

Home

{HOME}

Ins or Insert

{INSERT} or {INS}

Left arrow

{LEFT}

Num Lock

{NUMLOCK}

Page Down

{PGDN}

Page up

{PGUP}

Print Screen

{PRTSC} (reserved for future use)

Right-click

{RIGHT}

Scroll Lock

{SCROLLLOCK}

TAB

{TAB}

Up key

{UP}

F1

{F1}

F2

{F2}

F3

{F3}

F4

{F4}

F5

{F5}

F6

{F6}

F7

{F7}

F8

{F8}

F9

{F9}

F10

{F10}

F11

{F11}

F12

{F12}

F13

{F13}

F14

{F14}

F15

{F15}

F16

{F16}

Numeric keypad plus sign

{ADD}

Digital keyboard minus sign

{SUBTRACT}

Numeric keypad Multiplication

{MULTIPLY}

Numeric keypad Division

{DIVIDE}

To specify a key that can be used with any combination of Shift, Ctrl, and Alt keys, add one or more of the following code before the key code.

Key

Code

Shift

+

Ctrl

^

Alt

%

To specify any combination of Shift, Ctrl, and Alt keys when pressing keys, put the code of these keys in brackets. For example, to specify to press the Shift key simultaneously when pressing E and C, use "+ (EC )". To specify Shift when pressing E, and then Shift when pressing C, use "+ EC ".

To specify duplicate keys, use the format {key number }. A space must be placed between the key and number. For example, {LEFT 42} indicates 42 times of LEFT direction and {h 10} indicates 10 times of H.

(The above part is from Microsoft's msdn details can refer to: https://msdn.microsoft.com/zh-cn/library/system.windows.forms.sendkeys.send.aspx)

2) Three keys: It is troublesome to process the combination of the three keys. I have never succeeded in using the above method.

At this time, we may have to rewrite ProcessCmdKey.

1 protected override bool ProcessCmdKey (ref Message msg, Keys keyData)
2 {
3 if (keyData & Keys. Alt) = Keys. Alt) // alt is detected.
4 {
5 if (keyData & Keys. Control) = Keys. Control)
6 {
7 System. Windows. Forms. SendKeys. Send ("^ % ");
8 return true;
9}
10}
11 return base. ProcessCmdKey (ref msg, keyData );
12}


You need to debug it yourself.

 

3. Join our Technical Group to share more technologies.

 

Welcome to the technology sharing group:238916811

 



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.