更改默认按键操作 JavaScript

发布于 2024-12-11 06:03:40 字数 300 浏览 0 评论 0原文

可能的重复:
如何用 jQuery 替换击键?

我可以通过这种方式更改 JavaScript 中按键的默认字符。就像如果有人按下“E”按钮,它会打印出 ap ,这是实时的,就像它被输入到文本区域一样。无论如何,用 jQuery 或类似的东西来做到这一点?谢谢!

Possible Duplicate:
How do I replace a keystroke with jQuery?

Is there a way where I can change the default character of a key pressed in JavaScript. Like if somebody presses the "E" Button, it will print out a p instead, and this is live, so like its being typed into a textarea. Anyway to do this with jQuery or anything like that? Thnaks!

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

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

发布评论

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

评论(2

吃不饱 2024-12-18 06:03:40

这是一个工作解决方案的完整示例,基于 回答:

jQuery.fn.extend({
    insertAtCaret: function(myValue) {
        return this.each(function(i) {
            if (document.selection) {
                //For browsers like Internet Explorer
                this.focus();
                sel = document.selection.createRange();
                sel.text = myValue;
                this.focus();
            }
            else if (this.selectionStart || this.selectionStart == '0') {
                //For browsers like Firefox and Webkit based
                var startPos = this.selectionStart;
                var endPos = this.selectionEnd;
                var scrollTop = this.scrollTop;
                this.value = this.value.substring(0, startPos) + myValue + this.value.substring(endPos, this.value.length);
                this.focus();
                this.selectionStart = startPos + myValue.length;
                this.selectionEnd = startPos + myValue.length;
                this.scrollTop = scrollTop;
            } else {
                this.value += myValue;
                this.focus();
            }
        })
    }
});

$(function() {
    var ta = $('<textarea id="ta" style="width: 400px; height: 200px;"></textarea>').appendTo('body');
    ta.keypress(function(e) {
        if (e.which == 101) // e
        {
            e.preventDefault();
            ta.insertAtCaret('P');
        }
    });
});

Here is a complete example of a working solution, based on this answer:

jQuery.fn.extend({
    insertAtCaret: function(myValue) {
        return this.each(function(i) {
            if (document.selection) {
                //For browsers like Internet Explorer
                this.focus();
                sel = document.selection.createRange();
                sel.text = myValue;
                this.focus();
            }
            else if (this.selectionStart || this.selectionStart == '0') {
                //For browsers like Firefox and Webkit based
                var startPos = this.selectionStart;
                var endPos = this.selectionEnd;
                var scrollTop = this.scrollTop;
                this.value = this.value.substring(0, startPos) + myValue + this.value.substring(endPos, this.value.length);
                this.focus();
                this.selectionStart = startPos + myValue.length;
                this.selectionEnd = startPos + myValue.length;
                this.scrollTop = scrollTop;
            } else {
                this.value += myValue;
                this.focus();
            }
        })
    }
});

$(function() {
    var ta = $('<textarea id="ta" style="width: 400px; height: 200px;"></textarea>').appendTo('body');
    ta.keypress(function(e) {
        if (e.which == 101) // e
        {
            e.preventDefault();
            ta.insertAtCaret('P');
        }
    });
});
筱武穆 2024-12-18 06:03:40

查看此框架以添加键盘快捷键。

http://www.openjs.com/scripts/events/keyboard_shortcuts/

当然它不会自动将任何按键转换为另一个字符并输出该字符,您必须弄清楚页面的哪个部分具有焦点,例如文本字段,并用您实际想要输入的字符修改它的值。

Check out this framework for adding keyboard shortcuts.

http://www.openjs.com/scripts/events/keyboard_shortcuts/

Of course it won't automatically convert any keypress to another character and output that character, you will have to figure out what part of the page has focus, for instance a text field, and modify it's value with what ever character you actually want to be inputed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文