Sign in to follow this  
Followers 0
richard1017

Question: Perfectly mapping mouse button to keyboard key

6 posts in this topic

#1 ·  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

Share this post


Link to post
Share on other sites



#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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Get rid of the Sleep(10). This might be an issue of how the keyboard press naturally presses keys several times when you hold down a key.

Edited by computergroove

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Thank you, Geir1983.

Your correction works like a charm!

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0