Lua处理鼠标事件 [英] Lua handling mouse event

查看:895
本文介绍了Lua处理鼠标事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉Lua,只是为罗技鼠标编写一些lua脚本来玩游戏.....

I'm not familiar with Lua, just writing some lua script for logitech mouse to play game.....

这是我所期望的:当我按下鼠标上的某个键时,它开始按下键盘上的"1",而当我再次按下鼠标键时,它就停止了.

Here is what I expected: when I press some key on mouse, it began to press '1' on the keyboard, and when I press the mouse key again, it just stop.

这是我尝试过的:我使用全局标志来跟踪切换,但是一旦开始它就不会停止....我不知道lua如何处理事件,并且我认为全局标志不是一个好主意.那么有没有更好的方法来做到这一点?

And here is what I've tried: I use a global flag to keep track of the switch, but it won't stop once begin....I don't know how lua handle events, and I suppose global flag is not a good idea. So any better way to do this?

下面是代码:

on = 0
cd = 50

function shift_example() 
    while on do
        PressAndReleaseKey("1")
        Sleep(cd)
    end 
end


function OnEvent(event, arg) 
    OutputLogMessage("event = %s, arg = %s\n", event, arg) 
    if (event == "MOUSE_BUTTON_PRESSED" and arg == 5 and on ==0) then 
        OutputLogMessage("set on = true\n") 
        on = 1
        shift_example() 
    end 
    if (event == "MOUSE_BUTTON_PRESSED" and arg == 5 and on == 1) then 
        OutputLogMessage("set on = false\n") 
        on = 0
    end 

end

推荐答案

您的脚本正忙于运行循环.运行循环时,它将不处理任何事件.

Your script is busy running the loop. It will not process any events while running the loop.

每次在循环中使用 IsMouseButtonPressed(1)轮询按钮状态,并在下一次0-1转换时中断循环.您的更新率当然会受到延迟的限制.

Poll the button state every time using IsMouseButtonPressed(1) within your loop and break the loop on the next 0-1 transition. Your updaterate is of course limited by the delay.

通过小的循环来替换较长的延迟,这些循环经常以较短的延迟来检查按钮状态.

Replace longer delays by small loops that frequently check the button state with short delays.

添加了示例

我没有任何Logitech硬件,因此无法对其进行测试.单击鼠标按钮5时,此代码应开始向密钥1发送垃圾邮件,再次单击该代码应停止.这是一种非常简单的方法,不存储任何按钮状态.相反,它只是在按下按钮时执行某项操作,然后再执行相同的操作直到按下按钮.再次单击将导致第二个循环结束,您可以处理新事件.

I don't have any Logitech hardware so I can't test it. This code should start spamming Key 1 once you click mouse button 5 and stop once you click it again. It's a very simple approach that does not store any button states. Instead it just does something while the button is pressed and then does the same thing until the button is pressed. A second click will cause the second loop to end and you can process new events.

function Button5Loop()
   -- we are here because the button was pressed so it should still be pressed
   -- so we can start doing something as long as the button is pressed
   -- but this time we check the button state in every loop cycle
   while IsMouseButtoPressed(5) do
    PressAndReleaseKey("1")
    Sleep(50)
   end
   -- the button not pressed anymore so it has been released
   -- so we continue spamming in a second loop
   -- until the button is pressed again
   repeat
     PressAndReleaseKey("1")
     Sleep(50)
   until IsMouseButtonPressed(5) 
end

function OnEvent(event, arg) 
    OutputLogMessage("event = %s, arg = %s\n", event, arg) 
    -- mousebutton 5 has been pressed
    if event == "MOUSE_BUTTON_PRESSED" and arg == 5 then 
        Button5Loop() 
    end
end

这篇关于Lua处理鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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