echan101 Posted September 8, 2021 Share Posted September 8, 2021 Hi I've written a macro that tries to turn the spacebar into a PTT button - if you hold spacebar longer than 150ms, it will unmute Zoom, and then when you release spacebar it will mute Zoom again. This means that you can use the spacebar normally to type in any window, but as soon as you hold it down, then you can talk. It's really cool. It works! BUT I have a problem with it. When typing a normal space (less than 150ms), it sends the spacebar after you *release* spacebar instead of spacebar *down*, which means that sometimes it will reverse characters and spaces if you type quickly. Example: "Theq uickb rownf ox". So I need to be able to buffer the next key press after space bar, then play this key in order *after* the space. Any ideas how to do this? Here is the code: expandcollapse popup#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_UseUpx=n #AutoIt3Wrapper_Compile_Both=y #AutoIt3Wrapper_UseX64=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <Misc.au3> ; used by _IsPressed #include <MsgBoxConstants.au3> HotKeySet("{space}", "_SpaceBar") Local $hDLL = DllOpen("user32.dll") Func _SpaceBar () ConsoleWrite("Space Bar Key was pressed." & @CRLF) HotKeySet("{space}", "_ignore") ; to disable auto keyboard repeat ; Start timer Local $hTimer = TimerInit() ; Begin the timer and store the handle in a variable. Local $ptt = False While _IsPressed("20", $hDLL) If TimerDiff($hTimer) >= 150 and $ptt = False then ; Unmute $c = ControlFocus("[CLASS:ZPContentViewWndClass]","","") $c = ControlSend("[CLASS:ZPContentViewWndClass]", "", "", "!a") $ptt = True ConsoleWrite ($c) EndIf Sleep(10) WEnd Local $tDiff = TimerDiff($hTimer) ; Find the difference in time ConsoleWrite("_IsPressed - Space Bar Key was released." & $tDiff & @CRLF) If $tDiff < 150 Then ; It's a short press so it's a normal space HotKeySet("{space}") ; Unset the hotkey Send("{space}") EndIf If $tDiff >= 150 Then ; It's a long press so it unmuted and now we need to remute. $c = ControlFocus("[CLASS:ZPContentViewWndClass]","","") $c = ControlSend("[CLASS:ZPContentViewWndClass]", "", "", "!a") ConsoleWrite ($c) EndIf HotKeySet("{space}", "_SpaceBar") ; Re-enable hotkey EndFunc While 1 ; This is required to stop the program from using all CPU. sleep(30) Wend Func _ignore() EndFunc ;==>_ignore Func _Exit() DllClose($hDLL) Exit MsgBox(64 + 262144, Default, "Bye, bye", 1) EndFunc ;==>_Exit Link to comment Share on other sites More sharing options...
ViciousXUSMC Posted September 8, 2021 Share Posted September 8, 2021 Assuming you normally keep the Zoom window up when you use the feature you built the code for. You could just say "only do this if Zoom window is active" that way the spacebar would work normally in all other circumstances. If you keep the window hidden, you can then downgrade this to "does the process/window exist" Lastly you could just use a different key that is not normally very used like one of the F Keys. Link to comment Share on other sites More sharing options...
echan101 Posted September 9, 2021 Author Share Posted September 9, 2021 Hi I think I need to explain that the code works, there's just a minor problem that I'm sure could be solved but I just don't know how. I need to be able to buffer the next key press after space bar, then play this key in order *after* the space. Like I know there's a BlockInput function. Is there something similar that will record the next keystroke but not play it? That's all I need. Link to comment Share on other sites More sharing options...
seadoggie01 Posted September 9, 2021 Share Posted September 9, 2021 Your problem is a timing issue. It goes a little something like this: You start typing "The" and press space Your program jumps into action, starts the timer, and sees that it hasn't been 150ms yet. It sleeps for 10ms (maybe... actual results will vary, Sleep is not super accurate) You still have your thumb on space, but it starts to move up a bit. Your program has looped a few times, we're approaching 60ms You release space and your pinky starts heading down towards 'q'. Your program loops and realizes that you aren't pressing space. It loads the timer difference and writes to the console. Your pinky hits 'q' now Your program re-unsets the HotKeySet and finally sends space To fix this, you need to send the space as soon as humanly programmatically possible. ViciousXUSMC's suggestion was a good one: use a non-typing key. Consider using one of the number pad keys if you have them. If you insist on using space, maybe use it with a modifier key, like Ctrl? Alternately, have a typing mode activate by a complex keyboard shortcut, like Ctrl+Shift+T or something. When in typing mode, remove the space hotkey and reactivate it when you're done. Then you can use space without needing to hold it down, if you'd like Still insisting on just Space? Fine, but why are you asking for help if you're not even going to listen to it </Rant> Try pressing space while you're in Zoom. Does it do anything? If not, you have one final option: when you first detect that space was pressed, cancel the HotKey and send space there instead (ie: the very beginning of _SpaceBar). Then if you're typing you'll get the space immediately and if not, Zoom doesn't care about a space, so the mute button works All my code provided is Public Domain... but it may not work. Use it, change it, break it, whatever you want. Spoiler My Humble Contributions:Personal Function Documentation - A personal HelpFile for your functionsAcro.au3 UDF - Automating Acrobat ProToDo Finder - Find #ToDo: lines in your scriptsUI-SimpleWrappers UDF - Use UI Automation more Simply-erKeePass UDF - Automate KeePass, a password managerInputBoxes - Simple Input boxes for various variable types Link to comment Share on other sites More sharing options...
JockoDundee Posted September 9, 2021 Share Posted September 9, 2021 3 Thoughts - 1) Just resend the space right after you catch the hotkey - sure it will send a space to whatever window is open on mute/unmute - but maybe that’s not a big a deal is that in this case? 2) Reduce 150ms to 75ms and see if you can get away with it. 3) If you really need to do it, you can look at the code here, it basically does what you want - but it’s hideous: Code hard, but don’t hard code... 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