JavaScriptで「キーイベント」「マウスイベント」「加速度センサーイベント」を取得するプログラムのシンプルなひな形を作った。
マウスは、押したまま動かすとウインドウ左上を0,0としたXY座標を表示します。
サンプルを見る
一括ダウンロード
HTML部分
5 | < link rel = "stylesheet" href = "mystyle.css" > |
6 | < script src = "onEvent.js" ></ script > |
7 | < title >JavaScript de Event</ title > |
10 | < h1 >JavaScript de Event</ h1 > |
11 | < p >< strong >キーイベント・マウスイベント・加速度センサーイベント</ strong >を取得します</ p > |
12 | < p id = "display" >イベントをどうぞ!</ p > |
JavaScript部分
7 | document.getElementById( "display" ).innerHTML = str; |
11 | window.onkeydown = keyHandler; |
13 | function keyHandler(e){ |
14 | print( "keyCode = " + e.keyCode); |
18 | var isMouseDown = false ; |
20 | window.onmousedown = mouseDownHandler; |
21 | window.onmouseup = mouseUpHandler; |
22 | window.onmousemove = mouseMoveHandler; |
24 | function mouseDownHandler(e){ |
29 | function mouseUpHandler(e){ |
34 | function mouseMoveHandler(e){ |
40 | var r = e.target.getBoundingClientRect(); |
44 | print( "mouseXY = " + x + ", " + y); |
49 | window.ondevicemotion = motionHandler; |
51 | function motionHandler(e){ |
53 | var x = e.accelerationIncludingGravity.x; |
55 | var y = e.accelerationIncludingGravity.y; |
57 | var z = e.accelerationIncludingGravity.z; |
59 | print( "加速度センサー x: " + x + " y: " + y + " z: " + z); |
コメント