简单的javascript击键计数 [英] simple javascript keystroke count

查看:108
本文介绍了简单的javascript击键计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个在线学习/调查,我需要计算参与者的击键次数。我要求他们在文本字段中输入lrlrlrlrlrlrl ...来模拟步行。事实证明,许多参与者(正如在任务上花费的时间所证明的那样)正在复制和粘贴。

I'm developing an online study/survey where I need to count the number of keystrokes that a participant makes. I am asking them to type lrlrlrlrlrlrl... in a text field to simulate walking. Turns out many of the participants (as evidenced by the time spent on the task) are copying and pasting.

我需要一些可以计算击键次数的东西,这样我就能识别按要求完成任务的参与者。该研究是在Coldfusion中编程的,我正在考虑某种javascript / onkeydown / hidden文件字段组合,但我不是真正的程序员。

I need something that will count keystrokes so I can identify participants who completed the task as requested. The study is programmed in Coldfusion and I was thinking about some sort of javascript/onkeydown/hidden file field combination, but I am not really a programmer.

任何帮助将不胜感激。谢谢。

Any help would be appreciated. Thanks.

推荐答案

http ://jsfiddle.net/kBJGM/

HTML:

<input type="text" class="nopaste"/>
<input type="text" id="countstroke"/>
<span id="count"></span>​

Javascript :

Javascript:

var strokeCount = 0;

$(function(){

    $(".nopaste").bind("copy paste", function(e){
        e.preventDefault();
    });

    $("#countstroke").keyup(function(){
        $("#count").text("Count: " + (++strokeCount));
    });
});​

如果您想更进一步,可以强制只注册L和R键( http: //jsfiddle.net/kBJGM/5/ ):

If you want to take it a step further, you can enforce that only the L and R keys are registered (http://jsfiddle.net/kBJGM/5/):

$("#restrictivecount").keypress(function(e){
    var seq = rstrokeCount % 2;
    var allow = true;
    switch(e.keyCode){
        case 76:
        case 108: // L or l
            if (seq == 1) allow = false;
        break;
        case 82:
        case 114: // R or r
            if (seq == 0) allow = false;
        break;               
        default:
            allow = false;
        break;               
    }

    if (allow)
        $("#rcount").text("Count: " + (++rstrokeCount));
    else
        e.preventDefault();
});

这篇关于简单的javascript击键计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆