有没有JavaScript库来捕获鼠标/键盘事件,并将它们发送到外部服务器? [英] Is there any javascript library to capture mouse/keyboards events and send them to external server?

查看:124
本文介绍了有没有JavaScript库来捕获鼠标/键盘事件,并将它们发送到外部服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的是什么,其中:

What I need is something which:

  • 可以捕获所有的键盘事件
  • 可以捕获所有鼠标事件(点击,移动)
  • 可以捕获页面滚动,可能是照顾有关浏览器的差异
  • 发送使用JSONP数据外部服务器(或其他任何东西,但需要在最新的浏览器不但工作)
  • 是相当小的,顶多XX KB我希望

我想找到一些东西,至少有3以上的正确实施。 我还可以看看JS框架,如道场和JQuery的,如果他们能帮助我,但后来我将能够保持它足够小?

I would like to find something which have at least 3 of above implemented properly. I can also look at js frameworks like Dojo or JQuery if they can help me, but then will I be able to to keep it small enough?

推荐答案

如何自己写? :)让我们看看这个简单的jQuery code:

How about writing it yourself ? :) Let's consider this simple jquery code :

把这个在你的< HEAD>

put this in your < head >

    <script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(document).ready(function(){
            $('html').mousemove(function(event){
                console.log("mouse move X:"+event.pageX+" Y:"+event.pageY);
            });
            $('html').click(function(event){
                console.log("mouse click X:"+event.pageX+" Y:"+event.pageY);
            });
            $('html').keyup(function(event){
                console.log("keyboard event: key pressed "+event.keyCode);
            });
        });
    </script>

如果你会去到Firefox萤火虫或IE / Chrome开发工具/ JavaScript控制台,你会看到所有的值。您将需要实现简单的事件对象,你需要的数据,以及一些机构发布的数据收集每隔几秒钟(使用jQuery邮寄或AJAX方法和JSON对象)

If you'll go to Firefox firebug, or IE/Chrome developer tools / javascript console you'll see all the values. You will need to implement simple event object with the data you need, and some mechanism to post the gathered data every couple of seconds (using jquery post or ajax method and JSON object )

汇总:

  • 在所有的键盘事件?是的(关键presses)
  • 鼠标的移动和点击?是
  • 能赶上页面滚动?可行
  • 在可能采取在意浏览器的差异 - 这是jQuery的主要目标
  • 在使用JSONP将数据发送到外部服务器?确保只使用$阿贾克斯或$。员额
  • 是相当小的,顶多XX KB我希望 - jQuery的it'self约为31KB minfied / gzip压缩

这不是防弹的,你需要的服务器部分(简单的PHP / asp.net mvc的页面反序列化JSON并将其存储到数据库/ whaterver需要XML),你准备好去:)

It's not bulletproof, you'll need the server part (simple php/asp.net mvc page to deserialize json and store it into db / xml whaterver you need ) and you're ready to go :)

有关按在注释下了批处理数据 - 非常好的问题。量变到质变的.mousemove事件:

as per the comment below about batching up the data - very good point. Changeing the .mousemove event to :

            $('html').mousemove(function(event){
                console.log("mouse move X:"+event.pageX+" Y:"+event.pageY);
                var color = 'red';
                var size = '2px';
                $("body").append(
                    $('<div></div>')
                        .css('position', 'absolute')
                        .css('top', event.pageY + 'px')
                        .css('left', event.pageX + 'px')
                        .css('width', size)
                        .css('height', size)
                        .css('background-color', color)
                );
            });

会更容易想象有多少数据将是 - 每个点是一个POST到远程服务器

will make it easier to imagine how much data it will be - each dot would be a single POST to the remote server

这篇关于有没有JavaScript库来捕获鼠标/键盘事件,并将它们发送到外部服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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