richard1017 Posted December 21, 2014 Share Posted December 21, 2014 (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 December 21, 2014 by richard1017 Link to comment Share on other sites More sharing options...
computergroove Posted December 21, 2014 Share Posted December 21, 2014 #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 More sharing options...
richard1017 Posted December 21, 2014 Author Share Posted December 21, 2014 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. Link to comment Share on other sites More sharing options...
computergroove Posted December 21, 2014 Share Posted December 21, 2014 (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 December 21, 2014 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 More sharing options...
Solution Geir1983 Posted December 21, 2014 Solution Share Posted December 21, 2014 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 More sharing options...
richard1017 Posted December 21, 2014 Author Share Posted December 21, 2014 Thank you, Geir1983. Your correction works like a charm! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now