0

I am trying to make a script for logitech mouse that:

  1. When left mouse button is pressed, it will activate case 1
  2. When holding the right mouse button and pressing the left mouse button, it will activate the case 2

However, no matter how I try it will only work on case 1.

EnablePrimaryMouseButtonEvents(true)

function OnEvent(event, arg)
  -- Case 1: Press only Button  1
  if (event == "MOUSE_BUTTON_PRESSED" and arg == 1 and IsKeyLockOn("scrolllock") == false) then
    Sleep(77)   
    if (IsMouseButtonPressed(1)) then
      MoveMouseRelative(0, 4)
      Sleep(76) 
    end 
    if (IsMouseButtonPressed(1)) then
      MoveMouseRelative(0, 6)
      Sleep(62) 
    end
    if (IsMouseButtonPressed(1)) then
      MoveMouseRelative(0, 5)
      Sleep(84) 
    end

  --Case 2: Press button 1+2
  elseif (event == "MOUSE_BUTTON_PRESSED" and arg == 2 and IsKeyLockOn("scrolllock") == false) then
    Sleep(77)   
    if (IsMouseButtonPressed(1)) then
      MoveMouseRelative(0, 8)
      Sleep(76) 
    end 
    if (IsMouseButtonPressed(1)) then
      MoveMouseRelative(0, 9)
      Sleep(62) 
    end 
    if (IsMouseButtonPressed(1)) then
      MoveMouseRelative(0, 0)
      Sleep(84)
    end 
  end
end

I want to add one more case when I press RMB of this script:

  • When pressed RMB -> press Lshift button
  • When release RMB -> press Lshift button again

I added the end of the script as below, it does not work.

elseif (event == "MOUSE_BUTTON_PRESSED" and arg==2 and IsKeyLockOn("scrolllock")==false) then       
    PressAndReleaseKey("lshift")


elseif (event == "MOUSE_BUTTON_RELEASED" and arg==2 and IsKeyLockOn("scrolllock")==false) then
    PressAndReleaseKey("lshift")

if I want to add case 3: Press LAlt + LMB, so where do I put IsModifierPressed("lalt") ? I tried as below but failed

function OnEvent(event, arg)
  if (event == "MOUSE_BUTTON_PRESSED" and arg == 1 and not IsKeyLockOn("scrolllock")) then
    if not IsMouseButtonPressed(3) then -- 3 = Right Mouse Button (it's the same button as arg==2)
      -- Case 1: Press only LMB
    if IsModifierPressed("lalt") then 
      -- Case 3: Press LAlt+LMB
   else
      -- Case 2: Press RMB+LMB
    end
  elseif ((event == "MOUSE_BUTTON_PRESSED" or event == "MOUSE_BUTTON_RELEASED") and arg==2 and not IsKeyLockOn("scrolllock")) then
    PressKey("lshift") 
    Sleep(50)
    ReleaseKey("lshift") 
  end 
end
1
  • Actually the code doesn't do what you say. I'm not familar with the logitech software, but in your case you'll only get into the elseif (event == "MOUSE_BUTTON_PRESSED" if key 1 is not pressed (because of the elseif). Put the second statement as "normal" if-statement into the other and it will work.
    – csaar
    Commented Nov 14, 2019 at 7:43

2 Answers 2

3
function OnEvent(event, arg)
  if (event == "MOUSE_BUTTON_PRESSED" and arg == 1 and not IsKeyLockOn("scrolllock")) then
    if IsModifierPressed("lalt") then 
      -- Case 3: Press LAlt+LMB
    elseif not IsMouseButtonPressed(3) then -- 3 = Right Mouse Button (it's the same button as arg==2)
      -- Case 1: Press only LMB
    else
      -- Case 2: Press RMB+LMB
    end
  elseif ((event == "MOUSE_BUTTON_PRESSED" or event == "MOUSE_BUTTON_RELEASED") and arg==2 and not IsKeyLockOn("scrolllock")) then
    PressKey("lshift") 
    Sleep(50)
    ReleaseKey("lshift") 
  end 
end
4
  • Thanks for the help in advance: D I want to add one more case when I press RMB of this script: - When pressed RMB -> press Lshift button. - When released RMB -> press Lshift button again. I added the end of the script as below, it does not work, can you help me? elseif (event == "MOUSE_BUTTON_PRESSED" and arg==2 and IsKeyLockOn("scrolllock")==false) then PressAndReleaseKey("lshift") elseif (event == "MOUSE_BUTTON_RELEASED" and arg==2 and IsKeyLockOn("scrolllock")==false) then PressAndReleaseKey("lshift")
    – Kel
    Commented Nov 15, 2019 at 15:58
  • one more question pls :)
    – Kel
    Commented Nov 15, 2019 at 22:00
  • @Kel - Updated. Commented Nov 15, 2019 at 22:35
  • I have another question, hope you can help me xD link
    – Kel
    Commented May 9, 2020 at 5:07
-1

You must not have an end before an elseif. The elseif serves as the end of the last if automatically.

I'm surprised this code even compiles and runs at all, as you mentioned, that the first case does work.

Edit: You also do not need to use parentheses around the condition of an if. Since it is already encapsulated by if and then it was not necessary for the langugae design to make those mandatory as in other languages.

1
  • It's only bad formatted, the end closes the inner if.
    – csaar
    Commented Nov 14, 2019 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.