Tampermonkey未检测到钥匙按压

发布于 2025-01-23 14:43:40 字数 763 浏览 1 评论 0原文

我正在尝试在网页中执行此代码,如果按下移动键,它将警报“ Shift”。

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        ...
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @require      http://code.jquery.com/jquery-3.4.1.min.js
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    alert('Loaded!');


    var $ = window.jQuery;
    $(document).keypress(function(e) {
        if (e.which == 16) {
            alert("shift");
        }
    });

})();

该程序提醒“已加载!”但是,当我按Shift按钮时,请勿提醒“ Shift”。谁能帮我弄清楚问题在哪里?

I am trying to execute this code in a webpage, which will alert "shift" if it detects a shift key pressed.

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        ...
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @require      http://code.jquery.com/jquery-3.4.1.min.js
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    alert('Loaded!');


    var $ = window.jQuery;
    $(document).keypress(function(e) {
        if (e.which == 16) {
            alert("shift");
        }
    });

})();

The program alerts "Loaded!" but doesn't alert "shift" when I press the shift button. Can anyone please help me figure out where the problem is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不忘初心 2025-01-30 14:43:40

按键事件已弃用。改用键盘事件。

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://stackoverflow.com/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @require      http://code.jquery.com/jquery-3.4.1.min.js
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    alert('Loaded!');
    var $ = window.jQuery;
    $(document).on('keydown', function(e) {
        if (e.which == 16) {
            alert("shift");
        }
    });
})();

The keypress event has been deprecated. Use the keydown event instead.

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://stackoverflow.com/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @require      http://code.jquery.com/jquery-3.4.1.min.js
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    alert('Loaded!');
    var $ = window.jQuery;
    $(document).on('keydown', function(e) {
        if (e.which == 16) {
            alert("shift");
        }
    });
})();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文