如何将事件绑定到按住的鼠标左键?
我需要只要按住鼠标左键就执行命令。
I need a command to be executed as long as the left mouse button is being held down.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我需要只要按住鼠标左键就执行命令。
I need a command to be executed as long as the left mouse button is being held down.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
如果您希望在没有任何干预事件的情况下“发生某些事情”(即:无需用户移动鼠标或按任何其他按钮),您唯一的选择就是轮询。按下按钮时设置标志,释放按钮时取消设置。轮询时,检查标志并运行您的代码(如果已设置)。
这里有一些东西可以说明这一点:
但是,在 GUI 应用程序中通常不需要轮询。您可能只关心按下鼠标和移动时发生的情况。在这种情况下,只需将 do_work 绑定到
事件即可,而不是使用 poll 函数。If you want "something to happen" without any intervening events (ie: without the user moving the mouse or pressing any other buttons) your only choice is to poll. Set a flag when the button is pressed, unset it when released. While polling, check the flag and run your code if its set.
Here's something to illustrate the point:
However, polling is generally not necessary in a GUI app. You probably only care about what happens while the mouse is pressed and is moving. In that case, instead of the poll function simply bind do_work to a
<B1-Motion>
event.查看文档的表 7-1。有些事件指定按下按钮时的运动,
、
等。如果您不是在谈论按下-and-move 事件,然后您可以开始在
上执行活动,并在收到
时停止执行操作。Look at table 7-1 of the docs. There are events that specify motion while the button is pressed,
<B1-Motion>
,<B2-Motion>
etc.If you're not talking about a press-and-move event, then you can start doing your activity on
<Button-1>
and stop doing it when you receive<B1-Release>
.使用鼠标移动/运动事件并检查修改器标志。鼠标按钮将显示在那里。
Use the mouse move/motion events and check the modifier flags. The mouse buttons will show up there.