当前位置:网站首页>中文输入法输入事件composition的使用
中文输入法输入事件composition的使用
2022-06-11 11:29:00 【Michael18811380328】
中文输入法输入事件composition的使用
最近遇到一个问题:如果一个 INPUT 中,用户使用中文输入法,那么原生的键盘事件就会发生变化,无法获取正确的 keycode 及对应的输入内容。从前端的能力,只能操作浏览器相关的 API,不能获取中文输入法的内置 API。查阅资料后,可以使用 composition 事件来监听相关的时间。
不同中文输入法的情况可能大同小异(例如搜狗输入法,中文输入时,keycode 就是225,这样无法检测到点击的键)
composition 事件组合分成三个事件:compositionstart、compositionupdate、compositionend,分别对应中文输入法下,开始输入、更新输入、结束输入的事件。具体的细节如下。
文本合成系统如 input method editor(即输入法编辑器)开始新的输入合成时会触发 compositionstart 事件。
例如,当用户使用拼音输入法开始输入汉字时,这个事件就会被触发。
示例
// 首先获取INPUT元素,或者全局document元素
const inputElement = document.querySelector('input[type="text"]');
// 当输入区域获取焦点后,点击键盘,会触发 composition 事件组合,通过 event.data 可以获取输入的字符
inputElement.addEventListener('compositionstart', (event) => {
console.log(`generated characters were: ${
event.data}`);
});
下面可以显示三个不同事件的触发顺序
点击Input获取焦点,切换到中文输入法,点击键盘,然后触发下面事件:
首先触发一次 compositionstart 开始输入
然后触发一次 compositionupdate,如果继续点击键盘,那么继续触发这个事件

最后,点击空格键输入中文时,触发 compositionend 事件

const inputElement = document.querySelector('input[type="text"]');
const log = document.querySelector('.event-log-contents');
const clearLog = document.querySelector('.clear-log');
clearLog.addEventListener('click', () => {
log.textContent = '';
});
function handleEvent(event) {
log.textContent = log.textContent + `${
event.type}: ${
event.data}\n`;
}
inputElement.addEventListener('compositionstart', handleEvent);
inputElement.addEventListener('compositionupdate', handleEvent);
inputElement.addEventListener('compositionend', handleEvent);
参考文献
https://developer.mozilla.org/en-US/docs/Web/API/CompositionEvent
https://developer.mozilla.org/en-US/docs/Web/API/Element/compositionstart_event
https://www.jianshu.com/p/e9c837eba083
http://shenyujie.cc/2017/08/24/indirectInput/
https://blog.csdn.net/nosuchobjecterror/article/details/93193229
边栏推荐
- JS interview questions - arrow function, find and filter some and every
- 找到自己的优势,才能干活不累,事半功倍!
- MYCAT sub database and sub table
- WordPress regenerate featured image plugin: regenerate thumbnails
- Hamiltonian graph
- 【碎碎念】关于波长|波速|周期的想法
- [file upload vulnerability 06] server file content detection and bypass experiment + image horse production method (based on upload-labs-14 shooting range)
- JVM class loading process
- 設置默認收貨地址【項目 商城】
- Learn 02 - slice, morphological change and dimension exchange of numpy multidimensional array
猜你喜欢

An introduction to creating VOC datasets or Yolo datasets using labelimg

Gerber文件在PCB制造中的作用

ELK - ElastAlert最大的坑

The complete manual of the strongest Flink operator is a good choice for the interview~

适配器模式--能不能好好说话?

js面试题---箭头函数,find和filter some和every

Node连接MySql数据库写模糊查询接口

Typeerror: argument of type "Int 'is not Iterable

Exploration of kangaroo cloud data stack on spark SQL optimization based on CBO

命令模式--进攻,秘密武器
随机推荐
Recommend several gravatar avatar caching plug-ins
Node connects to MySQL database and writes fuzzy query interface
【C语言】anonymous/unnamed struct&&union
[file upload vulnerability 06] server file content detection and bypass experiment + image horse production method (based on upload-labs-14 shooting range)
AcWing 1353. Ski resort design (greedy)
WordPress landing page beautification plug-in: recommended by login Designer
How to form a good habit? By perseverance? By determination? None of them!
WordPress regenerate featured image plugin: regenerate thumbnails
企业微信小程序避坑指南,欢迎补充。。。
NFT digital collection system development and construction process
Don't be a fake worker
Etcd的运行时重配置
广东市政安全施工资料管理软件2022新表格来啦
Tu ne peux pas être libre sans richesse?
ELK - X-Pack设置用户密码
全国多年太阳辐射空间分布数据1981-2022年、气温分布数据、蒸散量数据、蒸发量数据、降雨量分布数据、日照数据、风速数据
在毕设中学习02——numpy多维数组的切片,形态变化,维度交换
js面试题---箭头函数,find和filter some和every
Mongodb usage
Create a folder in the WordPress Library