Jump to content

Question: Perfectly mapping mouse button to keyboard key


Go to solution Solved by Geir1983,

Recommended Posts

Posted (edited)

I'm trying to simulate the mouse button on a keyboard key. For example, {Space} as mouse left button. So, the {Space} button on keyboard should function exactly as the mouse left button:

Single click:   Press {Space}

Double click: Double press {Space}

Drag:            Hold down {Space} and move mouse (For example, drag a window)

Select:          Hold down {Space} and move mouse in text

The following is the AutoIt code I wrote to achieve this:

#include <Misc.au3>

HotKeySet("{SPACE}", "MouseLeft")

While 1
    Sleep(1000)
WEnd

Func MouseLeft()
    If _IsPressed('20') Then
        MouseDown("Primary")
        Do  
            Sleep(10)
        Until Not _IsPressed('20')
        MouseUp("Primary")
    EndIf
EndFunc

This code performs very well except the selection function. I tested selection in a Notepad and it seemed that the mouse click is repeatedly being sent when {Space} key is held down. I think this could be due to the keyboard built-in function as it will repeatedly send a key when the key is still pressed down after a delay period.

BTW, I also tried to achieve this by using AutoHotkey, and a single mapping did everything perfectly:

Space::LButton

So, how can I perfectly map a mouse button to a keyboard key in AutoIt?

Thank you so much for your attention and help.

Edited by richard1017
Posted

#include <Misc.au3> 
HotKeySet("{SPACE}", "MouseLeft") 

While 1     
     Sleep(1000) 
WEnd 

Func MouseLeft()     
     If _IsPressed('20') Then        
        Do
               MouseDown("Primary") 
               Sleep(10)
         Until Not _IsPressed('20')
         MouseUp("Primary")    
 EndIf 
EndFunc

Have you tried to move the mousedown("Primary")?

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Posted

Thank you, computergroove.

I just did a test in Notepad and moving the MouseDown("Primary") line doesn't solve the problem.

I think the trick is how to make the keyboard "do nothing" when the {Space} is kept pressed down. And I still don't know how to achieve this in AutoIt.

  • Solution
Posted

Try it like this, i think you keep calling the same function over and over when space is beeing pressed.

#include <Misc.au3>
HotKeySet("{SPACE}", "MouseLeft")
While 1
    Sleep(1000)
WEnd

Func MouseLeft()
    HotKeySet("{SPACE}", "_IgnoreSpace")
    MouseDown("Primary")
    Do
        Sleep(10)
    Until Not _IsPressed('20')
    MouseUp("Primary")
    HotKeySet("{SPACE}", "MouseLeft")
EndFunc

Func _IgnoreSpace()
    Sleep(10)
EndFunc

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...