1

私はjavascriptを使用してキーボードインターフェイスを設計しており、Shift+Rightkeyやctrl+tabなどのキーストロークの組み合わせを定義したいと考えています。しかし、ここに見られるように、javascriptをいじくり回した後、すべてのキーイベントが中断していることに気づきました。提供されている例では、右キーを押しながらシフトキーを押すと、右キーの機能が中断されます。

v = 1; /*v is the variable of velocity.*/

window.addEventListener("keydown", function(event)
{
    if(event.keyCode == 39) /*39 is the keycode of rightarrowkey.*/
    {
        //moves an element by the velocity.
        var keystroke = document.getElementById("keystroke");
        keystroke.style.left = parseInt(keystroke.style.left.slice(0,-2))+v+"px";
    }
    if(event.keyCode == 16) /*16 is the keycode of shift.*/
    {
         //increases the velocity of the element by four.
        document.getElementById("keystroke").style.borderColor = "red";
        v = 4;
    }
}, false); //but hitting the shiftkey while hitting the rightkey interrupts..!

また、オブジェクトを介してすべてのキーストロークを記録し、次に、ここに示すように、定義されたキーストロークに対して指定された間隔で繰り返されることを実験しまし。しかし、キーボードを処理するこのシステムは、個々のキーストロークを保持しません。キーを押すのが速すぎると、考慮されない場合があります。または、キーを長く保持しすぎると、考慮されない場合があります。

4

1 に答える 1

0

少し考えてみると、キーイベントの中断を処理しながら、各キーストロークを保持する方法を見つけたかもしれません。私のコードはやや風変わりですが、ユーザーからのキーストロークを管理するのにうまく機能します。

中断することなく各キーストロークを処理するための解決策は、私の質問で以前に提案されたように、オブジェクト内の各キーイベントを登録することでした。コードはこのオブジェクトを継続的に繰り返し、新しいキーイベントを処理します。

ただし、各キーストロークが保持され、少なくとも1回処理されるようにするには、キーイベントごとにキーバインディングを登録する必要があります。オブジェクトのスクリプトを拡張して、キーストロークとキーバインドの両方を登録しました。

var kbdin =
{
    stroked: new Object(),
    upbinded: new Object(),
    downbinded: new Object(),

    downbindKeystroke: function(keycode, functionality) {this.downbinded[keycode] = functionality;},
    upbindKeystroke: function(keycode, functionality) {this.upbinded[keycode] = functionality;},
    isDownbinded: function(keycode) {return this.downbinded[keycode];},
    isUpbinded: function(keycode) {return this.upbinded[keycode];},
    isStroked: function(keycode) {return this.stroked[keycode];},

    onDownstroke: function(event)
    {
        var keycode = event.keyCode;
        if(!this.isStroked(keycode))
        {
            this.stroked[keycode] = 1;
            if(this.isDownbinded(keycode))
            {this.downbinded[keycode]();}
        }
        if(this.isDownbinded(keycode))
        {event.preventDefault();}
    },

    onUpstroke: function(event)
    {
        var keycode = event.keyCode;
        delete this.stroked[keycode];
        if(this.isUpbinded(keycode))
        {
            this.upbinded[keycode]();
            event.preventDefault();
        }
    },

    handleKeystrokes: function()
    {
        for(var keycode in this.downbinded)
        {
            if(this.isStroked(keycode) > 5)
            {this.downbinded[keycode]();}
        }

        for(var keycode in this.stroked)
        {this.stroked[keycode]++;}
    }
};

document.addEventListener("keyup", function(event) {kbdin.onUpstroke(event);}, false);
document.addEventListener("keydown", function(event) {kbdin.onDownstroke(event);}, false);
window.setInterval(function() {kbdin.handleKeystrokes();}, 50);

これで、キーストロークとキーバインディングの両方が結合され、キーストロークのシグナリングとキーバインディングの実行の両方の機能を備えた各キーイベントがサポートされます。少し風変わりかもしれませんが、このコードは、個々のキーストロークを保持しながら、中断のないキーイベントを処理します。

于 2013-03-30T17:38:11.697 に答える