Suggestion Development Summary and summary of Keyboard Events (for Chinese Input Method status)

Source: Internet
Author: User
Important Keyboard Events:

Event Sequence: keydown-> keypress-> keyup

When the input method is Enabled:

Keypress:

The most special events among the three eventsSaid,If you use it skillfully, you can get twice the result with half the effort:

1.First, for most function keysKeypressEvent

Caps Lock,Shift,ALT,CTRL,Num LockYes. FortunatelyEnterOwn this event

2.For letters, numbers,PressReturnedKeycodeIs unreliable

InIEAndWebKitWhich of the following statements is returned:ASCII code

FirfoxAlways return0

However,Keyup keydownEvent key values are completely unified

3.KeypressOnly one character can be captured

KeydownAndKeyupYou can capture key combinations. Therefore, you may need to bind the key combination functionDown upEvent

When the Chinese Input Method is enabled, the browser has different corresponding events and solutions:

When the Chinese Input Method is Enabled:

Firefox:When you click the keys, the following events are triggered:

 Why is it virtual defocus?: This status does not actually trigger the defocus state, but shields all Keyboard Events bound to the input box.

IE,WebKit: However, when you click the keys, this will happen again.

 

Inspiration: in this state, you can capture keyup and keydown to capture keycode. The frontend can simulate the keycode input box to synchronize the input method with the input box, suggestion is synchronized when the input state is triggered.

 

 

So how can we avoid that when the input method is run, the user is hitting the return key to trigger the original event of the Return key?

After reading the two figures above, it's easy.------- KeypressBind the original event, such as triggering a search.

KeyupBind When the input method is running, press enter, space, and other events to be triggeredCan also be used to detect exiting the search box

 

So how can we check the point after the user inputs?

Determined by matching with the previous questionKeyupEvent ,If you need to block the event when the input method appears, you only need to bind it to keypress.The event is ready.

Pair Suggestion Summary:

Start:

InSuggestionIn development, the biggest obstacle is to check user behavior, and these user behaviors mainly converge on a small input box, clever use of keyboard events can be reducedCodeTo maximize the user experience.

 

The main user behaviors are summarized as follows:

 1.Change input content (add, delete, and paste)--The most important thing is continuous input.

 2.Hit function key--MainKeyValue:

Keycode: 13 -- Enter key

Keycode: 27 -- ESC key

Keycode: 38 --Up arrow key-->WebKitThe header is automatically located in the kernel. RememberPreventdefaultOh~

Keycode: 40 -- downstream key

Some minor problems encountered during development,And detours:

 First, I would like to summarize the following ideas:

1.Incorrect ideas:

One: each time a user clicks the keyboard, a request is sent.

This is undoubtedly a simple method, but it requires a lotAjaxAnd most of them do not present opportunities. Good front-end code should fully consider the virtual consumption in front-end interaction and minimize the virtual consumption.

 2. Check the content of the input box at intervals:

 Code execution at intervals of time is a waste of performance. The most important thing is that this mechanism does not have good control over user input events andJSCheck order,

 If the detection is completed exactly after the user input, an error occurs. Although it can be compensated by checking the status of the current input box, the user experience is poor and it consumes a lot of resources.AjaxRequest.

2After optimization:

BindKeyAfter hitting an event

After the development of the first method, I found that during self-testing, due to the failure to accurately monitor the timestamp of user behavior, a large numberBugAfter one repairs, a better idea emerged.

Pass DetectionKeyTo determine whether to send the request and block the continuous hitting event.

 Specific ideas:

When the user focuses on the input box, start monitoringKeydownEvent, which is recorded in the input box status.KeydownEvent and input box can change-"100Check the status of the current input box after milliseconds. If the status does not match the previous status, you can send it to the backend.AjaxRequest

 In this way, based on the user input, input frequency, and judgmentAjaxThe number of requests, and the threshold is used to reduce the number of requests.AjaxRequest. Many user inputs,JSThere are only a few detected,AjaxThere will be more, users will not move, there will be noJSDetection andAjaxThe input speed is low, which reduces the consumption.

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.