参考 mousePressed()

mousePressed()

当鼠标按钮被按下时调用一次的函数。

声明函数 mousePressed() 将设置一个代码块,在用户按下鼠标按钮时自动运行:

function mousePressed() { // Code to run. }

鼠标系统变量,如 mouseXmouseY,会在 p5.js 调用 mousePressed() 时更新为最新的值:

function mousePressed() { if (mouseX &lt; 50) { // Code to run if the mouse is on the left. } <p> if (mouseY &gt; 50) { // Code to run if the mouse is near the bottom. } } </p>

参数 event 是可选的。mousePressed() 总是传递一个 MouseEvent 对象,其中包含描述鼠标按压事件的属性:

function mousePressed(event) { // Code to run that uses the event. console.log(event); }

在触摸屏设备上,如果没有声明 touchStarted(),当用户的触摸开始时 mousePressed() 会运行。如果声明了 touchStarted(),那么当用户的触摸开始时 touchStarted() 会运行,而 mousePressed() 不会。

浏览器可能会将默认行为附加到各种鼠标事件上。例如,一些浏览器在用户按下鼠标按钮同时移动鼠标时会高亮显示文本。要防止此事件的任何默认行为,在函数的末尾添加 return false;

注意:mousePressed()mouseReleased()mouseClicked() 都是相关的。mousePressed() 在用户点击鼠标时立即运行。mouseReleased() 在用户释放鼠标点击时立即运行。mouseClicked()mouseReleased() 之后立即运行。

示例

语法

mousePressed([event])

参数

event

optional MouseEvent argument.

Notice any errors or typos? Please let us know. Please feel free to edit src/events/mouse.js and open a pull request!

相关参考