2011-10-27 14 views
2

私はHTML5を使用しており、マウスの移動中にマウスの右ボタンが押されているか左マウスボタンが押されているかを知りたい。マウスの右ボタンには、右ボタンのevent.button = 2があります。左ボタンにはevent.button = 0があります。mousemoveの間は常にevent.button = 0です。私はいくつかのサンプルコードを提供しました。私は間違って何をしていますか?mousemoveイベント中にマウスの右ボタンが押されているか左マウスボタンが押されているかを検出したい

<!DOCTYPE html> 

<html lang="en"> 

<head> 

<title>demo on detecting mouse buttons</title> 
<meta charset="utf-8"/> 

<style> 
#mycanvas{ border-style:solid; width:400px; height:400px; border-width:2px;} 
</style> 


<script type="text/javascript"> 

function detectDown(event) 
{ 
var string = "Mouse Down, event.button = " +event.button; 
var canvas = document.getElementById("mycanvas"); 
var context = canvas.getContext("2d"); 
context.clearRect(0,0,300,20); 
context.fillText(string,0,10); 
} 

function detectMove(event) 
{ 
var string = "Mouse Move, event.button = " +event.button; 
var canvas = document.getElementById("mycanvas"); 
var context = canvas.getContext("2d"); 
context.clearRect(0,30,300,20); 
context.fillText(string,0,40); 
} 

function detectUp(event) 
{ 
var string = "Mouse Up, event.button = " +event.button; 
var canvas = document.getElementById("mycanvas"); 
var context = canvas.getContext("2d"); 
context.clearRect(0,60,300,20); 
context.fillText(string,0,70); 
} 
</script> 

</head> 

<!-- --> 

<body> 

<canvas id="mycanvas"onmousedown="detectDown(event)" onmouseup="detectUp(event)" onmousemove="detectMove(event)" > 
</canvas> 

</body> 

</html> 
+0

これらの種類のテストやデモにはjsfiddle.comが便利です。 –

答えて

4

マウス移動イベントは、必ずしもボタンに依存しないです。ボタンが押されたかどうかに関係なく、マウスの移動を報告します。 mousedownに、どのボタンが停止しているかを追跡するフラグを作成する必要があります。 mouseupにリセットしてください。

関連する問題