Home Web Front-end JS Tutorial Listening to IME keyboard input events in JavaScript_javascript tips

Listening to IME keyboard input events in JavaScript_javascript tips

May 16, 2016 pm 06:06 PM
keyboard

How should the input method trigger keyboard events? Is the event triggered for every keystroke, or is the event triggered only after the word selection is completed? How can the entire sentence input trigger an event? Different operating systems and different browsers have different views on this. In the worst case, the browser only triggers keydown once after the user uses the input method, and then there are no keyboard events. This is a big problem for the implementation of the Suggestion control, because the Suggestion control needs to monitor changes in the text input box, and events are the most accurate and most computationally efficient method. If polling is used, performance may be affected.
First of all, to monitor keystroke events after enabling the input method, you should use the keydown event. This is the most informative event, because other keyboard events may not be triggered after the input method is enabled. Secondly, most operating systems and browsers have implemented a de facto standard, that is, when the user inputs using the input method, the keyCode value passed in the keydown event is 229. However, the frequency of triggering keydown is uncertain. In some cases, the event is triggered for every keystroke, and in other cases, the event is triggered only after the word selection is completed. At this time, if we still want to monitor the content changes of the text box in real time, we must use polling.

Copy code The code is as follows:

var timer;
var imeKey = 229;
function keydownHandler (e) {
clearInterval(timer)
if (e.keyCode == imeKey) {
timer = setInterval(checkTextValue, 50);
} else {
checkTextValue ();
}
}
function checkTextValue() {
/* handle input text change */
}

Opera is a fun browser It doesn't do anything that others do, and it likes to do things that others don't do. For example, it does not support the de facto standard of keyCode == 229, but uses keyCode == 197 to indicate the use of the input method. Therefore, you need to make some improvements based on the above code. If the Opera browser is detected, change the keyCode constant for comparison.
var imeKey = (UA.Opera == 0) ? 229 : 197;
Finally, there is an even less appreciated browser called Firefox for Mac. It is estimated that the Mac version is too unimportant to Mozilla, so there will be minor problems in the Mac version where many Windows versions have no problems, such as support for the above events. Firefox for Mac does not have keyCode == 229, and only the first keystroke after the input method is enabled will trigger the keydown event, so polling can only be used after a keystroke.
if (e.keyCode == imeKey || UA.Firefox > 0 && UA.OS == 'Macintosh') {
After adding these two improvements, real-time monitoring of text box changes is no longer possible The problem occurs even if the user has enabled the input method. The complete code is as follows:
Copy the code The code is as follows:

var timer;
var imeKey = (UA.Opera == 0) ? 229 : 197;
function keydownHandler (e) {
clearInterval(timer)
if (e.keyCode == imeKey || UA.Firefox > 0 && UA.OS == 'Macintosh') {
timer = setInterval(checkTextValue, 50);
} else {
checkTextValue();
}
}
function checkTextValue( ) {
/* handle input text change */
}
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to type underline on the keyboard? How to type only underline without typing? How to type underline on the keyboard? How to type only underline without typing? Feb 22, 2024 pm 07:46 PM

Adjust the input method to English and hold down the Shift key and the minus key. Applicable model of the tutorial: Lenovo AIO520C System: Windows 10 Professional Edition: Microsoft Office Word 2022 Analysis 1 First check the Chinese and English typing of the input method and adjust it to English. 2Then hold down the Shift key and the Minus key on your keyboard at the same time. 3 Check the interface to see the underlined words. Supplement: How to quickly enter underline in Word document 1. If you need to enter an underline in Word, select the space with the mouse, then select the underline type in the font menu to enter. Summary/Notes: Be sure to change the input method to English before proceeding, otherwise the underscore cannot be successfully entered.

How to open the right-click menu through shortcut keys How to open the right-click menu through shortcut keys Jan 14, 2024 pm 03:12 PM

When our mouse temporarily loses its function, how to use keyboard shortcuts to open the right-click menu? There are two methods. One is to press the Shift+F10 shortcut key to adjust the right-click menu, and the other is to use the keyboard shortcut between windows and ctrl. Directory keys can also be used. Let’s take a look at the specific tutorial below. The first method of using keyboard shortcuts to open the right-click menu: 1. When no file is selected on the computer desktop, press the button marked in red circle on the keyboard in the picture below. This button is the button to quickly open the right-click menu. 2. You can open the right-click menu on the desktop. If you need to select an item, just use the mouse to select it. Second method 1. In fact, we can use the "Shift+F10" shortcut key to adjust the right-click menu 2

How to fix Windows 11 keyboard not typing problem How to fix Windows 11 keyboard not typing problem Dec 28, 2023 pm 05:59 PM

Recently, some friends have encountered the problem of large characters on the win11 keyboard. They don't know which key to press to restore it. In fact, this may be because our keyboard is locked and we only need to unlock it. Which key should I press to recover if I can’t type on the win11 keyboard? 1. First, we press the “left shift+left alt+numlock” key combination on the keyboard. 2. Then, after opening the menu shown below, click the "Yes" button to enable the mouse keys. 3. Next, click the "Start Menu" or "Search" icon to open the taskbar. 4. After that, enter "osk" in the search box above to open the on-screen keyboard application. 5. Finally, click the "numlock" key in the lower right corner of the on-screen keyboard. Ps: If you are using a laptop, then

Keyboard auto-typing on Windows laptop Keyboard auto-typing on Windows laptop Feb 19, 2024 pm 05:33 PM

Computer input devices such as keyboard and mouse require human operation and cannot operate independently. The same goes for the touchpad and keyboard on Windows laptops. Text is not automatically entered, nor is the mouse clicked automatically. If something unusual happens, there must be a reason. If you encounter laptop keyboard auto-typing issues, follow the guide to fix it. Keyboard on Windows Laptop Typing Automatically When the keyboard on your Windows laptop is typing automatically, here’s how you can fix it. Check the keyboard manually Make sure the keyboard is working properly Check if the laptop is connected remotely Check if any autotyping programs are running Run a malware scan Adjust the keyboard settings Reinstall the keyboard driver Let’s know in detail

How to set up the keyboard boot function on a GIGABYTE motherboard (enable keyboard boot mode on GIGABYTE motherboard) How to set up the keyboard boot function on a GIGABYTE motherboard (enable keyboard boot mode on GIGABYTE motherboard) Dec 31, 2023 pm 05:15 PM

How to set up keyboard startup on Gigabyte's motherboard. First, if it needs to support keyboard startup, it must be a PS2 keyboard! ! The setting steps are as follows: Step 1: Press Del or F2 to enter the BIOS after booting, and go to the Advanced (Advanced) mode of the BIOS. Ordinary motherboards enter the EZ (Easy) mode of the motherboard by default. You need to press F7 to switch to the Advanced mode. ROG series motherboards enter the BIOS by default. Advanced mode (we use Simplified Chinese to demonstrate) Step 2: Select to - [Advanced] - [Advanced Power Management (APM)] Step 3: Find the option [Wake up by PS2 keyboard] Step 4: This option The default is Disabled. After pulling down, you can see three different setting options, namely press [space bar] to turn on the computer, press group

Win11 keyboard shortcut list Win11 keyboard shortcut list Jan 05, 2024 pm 02:46 PM

The win11 system has a large number of built-in keyboard shortcuts that allow us to perform certain operations quickly. However, many users not only don’t know what the win11 keyboard shortcuts are, but they are also not used to using them. Therefore, the editor will bring you a complete list of win11 keyboard shortcuts. Let’s learn them together. What are the win11 keyboard shortcuts: 1. F key F1 Show help all F2 Rename selected items all F3 Search files or folders all F10 Activate the menu bar in the active program All 2. Ctrl key Ctrl+A Select document or window All items All Ctrl+C Copy selected items All Ctrl+D Add webpage to your favorites All Ctrl+J Open Download Manager All Ctrl+L Highlight

How to assign Copilot buttons on any keyboard in Windows 11 How to assign Copilot buttons on any keyboard in Windows 11 Feb 20, 2024 am 10:33 AM

Windows 11 computer keyboards that will be launched in the next few months will have a new Copilot key. This key allows users to easily enter co-pilot mode by pressing a designated button without upgrading a new PC. This guide will detail how to set up the copilot button on any Windows 11 keyboard. Microsoft recently announced that they have made important progress in advancing artificial intelligence-assisted driving. Windows 11 will add a dedicated Copilot key to further enhance the experience of PC users. This hardware change represents the first major upgrade to Windows PC keyboards in thirty years. In the coming months, new Windows 11 computers will feature a new Copilot key design on the keyboard

Maicong K87 three-mode mechanical keyboard adds 'hyacinth axis' and 'ice cream axis' version: Gasket structure, initial price starts at 299 yuan Maicong K87 three-mode mechanical keyboard adds 'hyacinth axis' and 'ice cream axis' version: Gasket structure, initial price starts at 299 yuan Feb 29, 2024 pm 05:00 PM

According to news from this website on February 29, Maicong today launched two versions of "Hyacinth Switch" and "Glazed Ice Cream Switch" for the K87 three-mode mechanical keyboard. The keyboard features "Gasket structure, 80% arrangement", related shaft keyboards The price information is as follows: "Hyacinth Switch" version: initial price is 299 yuan. "Liuguang Ice Cream Switch" version: initial price is 379 yuan. According to reports, the series of keyboards use Gasket structure, 87-key 80% arrangement, full-key rollover, and support thermal For plugging and unplugging, it claims to use "original/MDA two-color PBT keycaps", uses a 1.2mm single-key slotted PCB (lower lamp position), is equipped with RGB lighting effects, and has a magnetic absorption nameplate design. In addition, this keyboard is equipped with a 6000 mAh battery and a wireless delay of 3ms. The official size and size of the keyboard have not been announced.

See all articles