Jump to content

Question: Perfectly mapping mouse button to keyboard key


Go to solution Solved by Geir1983,

Recommended Posts

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
Link to comment
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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Solution

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
Link to comment
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
 Share

  • Recently Browsing   0 members

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