【问题标题】:javascript input from keyboard without "prompt()"method popup (like pascal readln or gwbasic input or c++ scanf/gets/cin ...)没有“prompt()”方法弹出的键盘输入 javascript (如 pascal readln 或 gwbasic 输入或 c++ scanf/gets/cin ...)
【发布时间】:2015-03-13 21:13:31
【问题描述】:
嗨,javascript可以实现从键盘读取输入的功能吗?
像 pascal readln、gwbasic 输入、C scanf/gets、C++ cin?

只需完成以下内容:

    
    &ltscript>函数输入(){
    /* ??????????????? */
    }
    &lt/脚本>
    &lt/头>
    
    
    alert("输入内容");
    一个=输入();
    alert("你输入了:"+a);
    &lt/脚本>

我是一个绝对的初学者,我只是试图从一些书中复制和粘贴一些代码,但没有成功。

天呐!

【问题讨论】:

    标签: javascript input keyboard


    【解决方案1】:

    是的,您可以在从浏览器发送按键事件后调用函数:

    window.onkeypress = function(key) {
        alert(key.charCode);
    };
    

    将返回可以使用此列表翻译的字符代码:http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

    这个library 可能会帮助您进行翻译。然后它看起来像:

    window.onkeypress = function(key) {
        alert(GetChar(key.charCode));
    };
    

    编辑:好的,您想在按下“Enter”时输出序列。为此,您可以使用数组来存储按下的键:

    var chars = [];
    window.onkeypress = function(key) {
        var keyName = GetChar(key.charCode);
        if (keyName !== 'enter') {
            // store the current keyName
            chars.push(keyName);
        } else {
            // output the pressed chars as string
            alert(chars.join(''));
            // clear chars array
            chars = [];
        }
    };
    

    【讨论】:

    • 谢谢。虽然,当 RETURN 被击中时,我不知道如何读取整个键序列。在我的目的中,input() 应该读取键序列,a=input() 应该将整个序列(直到返回)加载到 a。
    • @GiuseppinaRossi:我用一个例子更新了上面的帖子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    • 2010-11-03
    相关资源
    最近更新 更多