javascript - 如何模拟按下 END 键
问题描述
我有一个从 aspx.vb 文件中调用的 javascript 函数:
using ScriptManager.RegisterStartupScript(Me, Page.GetType, "Script", "pressKey();", True)
我需要该pressKey
功能来模拟END按键,就像用户在键盘上按下它一样。
解决方案
End 键的 charCode 是 35。CSS 技巧有一个很好的字符代码列表:https ://css-tricks.com/snippets/javascript/javascript-keycodes/
这是 Vanilla JS 解决方案:
var keyboardEvent = document.createEvent("KeyboardEvent");
var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ?
"initKeyboardEvent" : "initKeyEvent";
keyboardEvent[initMethod](
"keydown", // event type : keydown, keyup, keypress
true, // bubbles
true, // cancelable
window, // viewArg: should be window
false, // ctrlKeyArg
false, // altKeyArg
false, // shiftKeyArg
false, // metaKeyArg
35, // keyCodeArg : unsigned long the virtual key code, else 0
0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
);
document.dispatchEvent(keyboardEvent);
推荐阅读
- angular - Angular:Async Await-Promise 中的 EventListener
- c - 为什么我的 C 代码没有只使用一个 get() 来获取第一个输入
- javascript - 接受本地 Apollo 的 RESTDataSource 上的自签名证书
- xamarin.forms - 如何将 DataGrid 列绑定到所选项目的内部列表
- sql - 谁能解释“WHERE CAST(SUBSTR(M.year,-4) AS UNSIGNED) < 1970”和“m.year<1970”之间的区别
- javascript - 在我的情况下,如何获取使用 java 脚本单击了哪个元素
- android - 我的旧图片应用停止在 Android 11 S20 上运行。BitmapFactory 的 outWidth 为 -1
- javascript - 如何从反应前端应用程序打开一个新选项卡
- javascript - passport.js 谷歌登录上的错误重定向
- arrays - Typescript:数组类型取决于先前的数组元素