[英]JS Function Not working from KeyCode / How can i call this JS function with keyboard key
Here is a simple script function getSelectedText()
that is working on button
click.这是一个正在单击
button
的简单脚本 function getSelectedText()
。 It means that when we select any text and click on the button, the function is creating a new NEWCLASS
div successfully.这意味着当我们 select 任何文本并单击按钮时,function 正在成功创建一个新的
NEWCLASS
div。 But now I want to use a shortcut key, like CTRL + W to get selected text in the NEWCLASS
div.但现在我想使用快捷键,如CTRL + W来获取
NEWCLASS
div 中的选定文本。
I tried this code but it doesn't work.我试过这段代码,但它不起作用。 Please, check it and let me know what mistake I am making here.
请检查一下,让我知道我在这里犯了什么错误。
var input = document.getElementsByTagName("body")[0];
input.addEventListener("keyup", function(event) {
if (event.keyCode === 37) {
event.preventDefault();
document.getElementById("myBtn").click();
}
});
My code:我的代码:
// Function to get the Selected Text function getSelectedText() { var selectedText = ''; // #### create a new element with variable (nw) #### // var nw = document.createElement("div"); // Element's tag nw.className = "NEWCLASS"; // Element's class name // some applied style // window.getSelection if (window.getSelection) { selectedText = window.getSelection(); } // document.getSelection else if (document.getSelection) { selectedText = document.getSelection(); } // document.selection else if (document.selection) { selectedText = document.selection.createRange().text; } else return; // #### get the Selected text appended to body #### // nw.innerHTML = selectedText; document.getElementsByClassName('maintitle')[0].prepend(nw); // Append element to body }
<button id="mybtn" onclick="getSelectedText()">Button</button> <p>Select any part of this sentence and press the button. Select any part of this sentence and press the button. Select any part of this sentence and press the button</p> <div class="maintitle"> </div>
the ctrl key with w will close the chrome browser so I used "Z" key for that you can replace the key code with whatever you want.带有 w 的 ctrl 键将关闭 chrome 浏览器,所以我使用“Z”键,您可以用任何您想要的替换键代码。
Find the keycodes here 在这里找到键码
const keySelected = new Set(); document.addEventListener('keydown', (e) => { keySelected.add(e.which); if(keySelected.has(17) && keySelected.has(90)){ getSelectedText(); } }); document.addEventListener('keyup', (e) => { keySelected.delete(e.which); }); /* //jquery code if anyone want $(document).ready(function(){ const keySelected = new Set(); $(document).keydown(function (e) { keySelected.add(e.which); if(keySelected.has(17) && keySelected.has(90)){ getSelectedText() } }); $(document).keyup(function (e) { keySelected.delete(e.which); }); }); */ // Function to get the Selected Text function getSelectedText() { var selectedText = ''; // #### create a new element with variable (nw) #### // var nw = document.createElement("div"); // Element's tag nw.className = "NEWCLASS"; // Element's class name // some applied style // window.getSelection if (window.getSelection) { selectedText = window.getSelection(); } // document.getSelection else if (document.getSelection) { selectedText = document.getSelection(); } // document.selection else if (document.selection) { selectedText = document.selection.createRange().text; } else return; // #### get the Selected text appended to body #### // nw.innerHTML = selectedText; document.getElementsByClassName('maintitle')[0].prepend(nw); // Append element to body }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="mybtn" onclick="getSelectedText()">Button</button> <p> Select any part of this sentence and press the button. Select any part of this sentence and press the button. Select any part of this sentence and press the button </p> <div class="maintitle"> </div>
You don't need to recall the event on the button, you should be capable of detect the press of the key you want and call the function, like:您不需要调用按钮上的事件,您应该能够检测到您想要的键的按下并调用 function,例如:
document.addEventListener('keydown', (e) => {
if (e.ctrlKey && e.code === 'keyW') {
getSelectedText();
}
});
Ctrl+W is not such a good choice, as on Windows it is a common shortcut key for closing the current Window or document, so your script will not even get the key event. Ctrl+W不是一个很好的选择,因为在 Windows 上它是关闭当前 Window 或文档的常用快捷键,所以你的脚本甚至不会得到关键事件。
But I'll pick Ctrl+Y instead.但我会选择Ctrl+Y代替。
You should not use the deprecated keyCode
property -- nor the which property which another answer suggests -- notice the red notice in the MDN documentation.您不应使用已弃用的
keyCode
属性——也不要使用另一个答案建议的which属性——请注意 MDN 文档中的红色通知。
It is even easier with the key
property.使用
key
属性更容易。 And you can use the ctrlKey
property to know whether the control key is down.并且您可以使用
ctrlKey
属性来知道控制键是否按下。
I would use the keydown instead of the keyup event, as you can then cancel any default effect of the key, with a call to cancelDefault
.我会使用 keydown 而不是 keyup 事件,因为您可以通过调用
cancelDefault
来取消该键的任何默认效果。
document.addEventListener("keydown", (event) => { if (event.ctrlKey && event.key == "y") { event.preventDefault(); getSelectedText(); } }); // Your original code: function getSelectedText() { var selectedText = ''; var nw = document.createElement("div"); // Element's tag nw.className = "NEWCLASS"; // Element's class name if (window.getSelection) { selectedText = window.getSelection(); } else if (document.getSelection) { selectedText = document.getSelection(); } else if (document.selection) { selectedText = document.selection.createRange().text; } else return; nw.innerHTML = selectedText; document.getElementsByClassName('maintitle')[0].prepend(nw); }
<button id="mybtn" onclick="getSelectedText()">Button</button> <p>Select any part of this sentence and press the button or enter Ctrl+Y. Select any part of this sentence and press the button or enter Ctrl+Y. Select any part of this sentence and press the button or enter Ctrl+Y. </p> <div class="maintitle"> </div>
This should work这应该有效
var input = document.getElementsByTagName("body")[0];
input.keypress("w", function(event) {
if (event.ctrlKey) {
event.preventDefault();
document.getElementById("myBtn").click();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.