Enter, press enter, and switch the input focus. The implementation idea is compatible with the codes of various browsers. Enter focus

Source: Internet
Author: User

Enter, press enter, and switch the input focus. The implementation idea is compatible with the codes of various browsers. Enter focus

During project creation, the customer requires that the input (Focus) can be directly switched using the enter key, and the information can be submitted directly at the last time.

The first idea is to copy a piece of code online and use it directly. However, Baidu and Google searched again and found the same code above 80%. Some codes are too old to be used. Some of them can only be used by some browsers. After half an hour, there is no proper solution. Finally, let's start it on your own.

I. Ideas

Each time you press enter, you can get the current focus position and set its next element to get the focus;

Ii. Code

<Script type = "text/javascript"> $ ('input: text: first '). focus (); document. onkeydown = function enterHandler (event) {var inputs = $ ("input"); // you can add other filter conditions var browser = navigator. appName; // browser name var userAgent = navigator. userAgent; // obtain the browser's userAgent string var Code = ''; if (browser. indexOf ('internet')>-1) // IE Code = window. event. keyCode; else if (userAgent. indexOf ("Firefox")>-1) // Firefox Code = event. which; e Lse // other Code = event. keyCode? Event. keyCode: event. which? Event. which: event. charCode; if (Code = 13) // you can add other filter conditions {for (var I = 0; I <inputs. length; I ++) {if (inputs [I]. id = document. activeElement. id) {I = (inputs. length-1 )? -1: I; $ ('#' + inputs [I + 1]. id). focus () break ;}}</script>

Because IE and Firefox have different key values, they make a simple judgment on the browser. In this way, you can obtain the hitting key value in each browser.

Finally, after obtaining the current value, you can add other conditions.


How to Use the carriage return key in VC ++ to switch the input focus

In Windows, the Tab key is the default key for switching the input focus, But switching the input focus with the Enter key is more in line with people's input habits. In Delphi and VB, this problem is easy to solve, but it is rarely mentioned in VC ++. Through repeated attempts, I found a method that may be helpful to everyone.
1. appwizard in VC ++ generates a dialog box-based program. Switch the resourseview status, insert an edit box in the dialog box, modify its styles attributes, and select multiline and want return.
2. ① Select add class in classwizard to add a new class. The name is defined as myedit, and the base class is selected as edit. The remaining items use the default options.
② Add the wm-keydown message ing function to the newly generated my-edit class and add the following code.
Oid Myedit: OnKeyDown (UINT nChar, UINT nRepCnt, UINT nFlags ){
// TODO: Add your message handler code here and/or call default
If (nChar = VK_RETURN) // Insert the start part {
CDialog * Pwnd = (CDialog *) GetParent (); // get the dialog box pointer
Pwnd-NextDlgCtrl (); // switch to the next input focus}
// Insert the end part
3. Select the member variables option in classwizard, select the class name dialog box class, select the ID of the edit box in control ids, and then select add variable.

After you enter the information in the 1st text boxes, press the Enter key. The program can automatically convert the focus to the next text box, so that you do not have to use the mouse.

This method is feasible, but it is not very good. We recommend that you use text box groups.
Text (0). text ---- text (n). text
When the value is 13, text (index + 1). SetFocus
In this way, you can accurately set the next text box so that it will not run in disorder.
Sendkeys needs to be placed in order.

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.