-1

Could someone please tell me how to edit a script in AutoHotkey?

I use a macro recorder, to perform several repetitive tasks, i.e. "MOVE UP n MOVE DOWN" many times, so I use to press the macro shortcut repeatedly. The issue is that in the macro recorder that I use if you assign a shortcut with a modifier to call a macro (i.e. Shift A), then you must release the modifier each time you want to perform the macro, so I usually have to (press Shift, press A, release Shift and release A), each time I want to call the macro, instead of just press Shift, hold it, and then press and release A many times. which is easier. I I am looking for an AutoHotKey script that could solve this issue, I have lots of this macros, so I can not create a bat or exe file for each one, I basically need a AutoHotkey script like:

if keystate, SHIFT, P
a::
send (Shift down)
send (a down)
send (Shift up)
send (a up)

or any other script that could send the Shift and A keys every time I press A but only if the Shift key is pressed.

I have been trying with loops, but I just don't know how to make it stop.

Consider that I need not to be obliged to release and press again "Shift + a" every time I want to call a macro, but also "Alt + a", and also "Alt + Win + a", each different combination call different macros. So it will be bad idea to set the "a" key to be always "Shift + a", because I need the "a" key to perform different macros according to which other modifier keys are pressed at same time.

The list of hotkeys that I use in this application are (everyone do something different, and it can not be replaced by any other hotkey)

Alt + A
Shift + A
Win + A
Alt + Shift + A
Win + Alt + A

and so on with most of the letters (S, D, Q, W, E, etc. and numbers 1, 2, 3,etc.) also:

Shift + Tab
Win + Tab
Alt + Capslock
Win + Alt + Capslock

(Its really annoying to got to release Shift and Tab, and press them both again, each time I want to run a macro, I run this macro many times repetitively) I think those are all the hotkeys, they all do different things that I run repetitively every time.

Please, I do need a script that would launch different actions pressing the same "a" key according to which other modifier keys are pressed at same time.

Hi Robert, I didn't understand quite well what you said, but I edited the script like this

#MaxThreadsPerHotkey 2

#SingleInstance Force
#installKeybdHook
#Persistent
AppName=MyKeys
Menu, Tray, Tip, %AppName%
Menu, Tray, Icon , Shell32.dll, 45, 1 ; 45, 28, 113, 74, 134
TrayTip, %AppName%, Started, 1 


*a::
GoSub, MyGetKeyStateSub
If (MyGetKeyStateValue=0)
{
    SendInput, a
}
Else
{
    if( MyGetKeyStateValue & 1 > 0) ; Compare first bit
        send {LAlt down}
        send {d down}
        send {LAlt up}
        send {d up}
    if( MyGetKeyStateValue & 10 > 0) ; compare second bit
        MsgBox, RShift
    if( MyGetKeyStateValue & 100 > 0) ; compare third bit
        send {LAlt down}
            send {d down}
        send {LAlt up}
        send {d up}
    if( MyGetKeyStateValue & 1000 > 0)
        MsgBox, RShift
    if( MyGetKeyStateValue & 10000 > 0)
        MsgBox, LAlt
    if( MyGetKeyStateValue & 100000 > 0)
        MsgBox, RAlt
    if( MyGetKeyStateValue & 1000000 > 0)
        MsgBox, LWin
    if( MyGetKeyStateValue & 10000000 > 0)
        MsgBox, RWin
    if ( MyGetKeyStateValue & 00000011 > 0) ; If LCtrl AND LShift pressed
        MsgBox, LCtrl AND LShift
}
Return


MyGetKeyStateSub:
MyGetKeyStateValue:=0
if getkeystate("LCtrl", P)
    MyGetKeyStateValue+=1 ; set least significant bit
if getkeystate("RCtrl", P)
    MyGetKeyStateValue+=2 ; set 2nd least significant bit, etc, etc.
if getkeystate("LShift", P)
    MyGetKeyStateValue+=4
if getkeystate("RShift", P)
    MyGetKeyStateValue+=8
if getkeystate("Lalt", P)
    MyGetKeyStateValue+=16
if getkeystate("Ralt", P)
    MyGetKeyStateValue+=32
if getkeystate("LWin", P)
    MyGetKeyStateValue+=64
if getkeystate("RWin", P)
    MyGetKeyStateValue+=128
Return



*d::
GoSub, MyGetKeyStateSub
If (MyGetKeyStateValue=0)
{
    SendInput, d
}
Else
{
    if( MyGetKeyStateValue = 1) ; LAlt
    {
        SendInput, {LAlt Down}d{LAlt Up}
        Return
    }
    if( MyGetKeyStateValue = 4) ; LWin
    {
        SendInput, {LAlt Down}{LWin Down}d{LAlt Up}{LWin Up}
        Return
    }
}
Return

and open the program, and the same it didn't work, first time I press Alt D I get the macro, second time I press D holding the alt key, I get a plain D.

I was thinking about what you said relative to the XY problem, perhaps I should rebuild all my macros from HK4 (Hot Keyboard Pro 4) to AutoHotkey. There are lots of macros that I created with the help of the HK4 GUI, but the macros themselves are stored inside an xml script file, I opened this file once with a text editor and its huge!, How much time could take me to script all these macros inside AHK?

I have set like 25 variables that apply to every macro (like 200 macros) mostly are mouse movements, mouse clicks, copy selected text (number values) from floating windows inside an application, apply arithmetic operation to them and pasting the results inside the same boxes again (HK4 has and option that play keys, because otherwise it can't not paste inside the application), running depending on which software, which floating windows names, and which variables are enabled or disable (inverted condition), also have toggles macros that (toggle between two macros with one hotkey), I mean, how much it will take me to learn how to script all those kind of macros inside AHK?, I looked on internet, but its very hard to understand each posted case that do not apply exactly to what I intend to do, Thanks Robert, what do you suggest me? I mean, I would rebuild all my macros in AHK because inside AHK I do not have to release the modifier key to perform a macro many times simultaneously, What about Autoit its easier to learn? I read it had macrorecorder? Thanks.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
eduardobedoya
  • 37
  • 5
  • 13

2 Answers2

0

How About this?

:*:a::+a

If you want to make this fancy, you could encapsulate this in a #IfWinActive statement to only get the shift a when THAT specific application is active, otherwise all a's will become Shift A's all the time...

How about this:

:*:a::
    Send, {Shift Down}a{Shift Up} ; or use the more modern SendInput
Return

Here is a little example how you could do this. Not sure if this meets your requirements. In the example I only created this for the letter a, but you can extend this by repeating this for other letters.

#SingleInstance Force
#installKeybdHook
#Persistent
AppName=MyKeys
Menu, Tray, Tip, %AppName%
Menu, Tray, Icon , Shell32.dll, 45, 1 ; 45, 28, 113, 74, 134
TrayTip, %AppName%, Started, 1 


*a::
GoSub, MyGetKeyStateSub
If (MyGetKeyStateValue=0)
{
    SendInput, a
}
Else
{
    if( MyGetKeyStateValue & 1 > 0) ; Compare first bit
        MsgBox, LCtrl
    if( MyGetKeyStateValue & 10 > 0) ; compare second bit
        MsgBox, RCtrl
    if( MyGetKeyStateValue & 100 > 0) ; compare third bit
        MsgBox, LShift
    if( MyGetKeyStateValue & 1000 > 0)
        MsgBox, RShift
    if( MyGetKeyStateValue & 10000 > 0)
        MsgBox, LAlt
    if( MyGetKeyStateValue & 100000 > 0)
        MsgBox, RAlt
    if( MyGetKeyStateValue & 1000000 > 0)
        MsgBox, LWin
    if( MyGetKeyStateValue & 10000000 > 0)
        MsgBox, RWin
    if ( MyGetKeyStateValue & 00000011 > 0) ; If LCtrl AND LShift pressed
        MsgBox, LCtrl AND LShift
}
Return


MyGetKeyStateSub:
MyGetKeyStateValue:=0
if getkeystate("LCtrl", P)
    MyGetKeyStateValue+=1 ; set least significant bit
if getkeystate("RCtrl", P)
    MyGetKeyStateValue+=2 ; set 2nd least significant bit, etc, etc.
if getkeystate("LShift", P)
    MyGetKeyStateValue+=4
if getkeystate("RShift", P)
    MyGetKeyStateValue+=8
if getkeystate("Lalt", P)
    MyGetKeyStateValue+=16
if getkeystate("Ralt", P)
    MyGetKeyStateValue+=32
if getkeystate("LWin", P)
    MyGetKeyStateValue+=64
if getkeystate("RWin", P)
    MyGetKeyStateValue+=128
Return

Eduard, here is an example which seems to work fine in my situation, it might be that your application is looking at the physical keys being pressed. In that case I would go for a look where you monitor the state of the window you are controlling, to see if the look needs to stop or continue. In this example I left the sub-routine out as that did not change (but you would need it here).

*d::
GoSub, MyGetKeyStateSub
If (MyGetKeyStateValue=0)
{
    SendInput, d
}
Else
{
    if( MyGetKeyStateValue = 1) ; LCtrl
    {
        ;MsgBox, LCtrl
        SendInput, {LShift Down}d{LShift Up}
        Return
    }
    if( MyGetKeyStateValue = 4) ; LShift
    {
        ;MsgBox, LShift
        SendInput, {LCtrl Down}{LShift Down}d{LShift Up}{LCtrl Up}
        Return
    }
}
Return
Robert Ilbrink
  • 7,738
  • 2
  • 22
  • 32
  • >( sorry it does not work. Its the same, I still have to release the modifier each time I want to perform the macro. Any other idea??? I supose I need a script that send the keycodes (a depper code). – eduardobedoya Feb 14 '14 at 18:16
  • Robert, thx for your Attention, inside the App that I want to use ur script seems to works fine, despite I got to type filenAmes with cApitAl letters. But there is a huge trouble, could you please tell me, how to modify your script in order to solve my problem postest above but with one and two modifiers at the same time?? I mean, I need not to be obliged to release and press again: Shift + A, but also Alt + A, and also Alt + Win + A (each different combination call different macros) but with your script I only got Shift + A always, no mather what other modifier keys are pressed.ThanksAdvanced – eduardobedoya Mar 02 '14 at 00:23
  • please read above I added a paragraph in the end, to explain better my question, thanks advanced Robert, I think I need another kind of script that take in account which modifiers keys are been holding down while the "a" key is pressed. Thanks Advanced. – eduardobedoya Mar 02 '14 at 00:34
  • I suggest, you list the quick-key combinations that you would like to use in this application. Then we can look at the easiest way to create short-cuts for these combinations. I am thinking of e.g. F1 for Shift-A, F2 for Alt-Shift-A, etc. but these keys may already be in use, or may not be convenient, you could also look at Numpad1 through Numpad0. To prevent your lowercase a ALWAYS turning into capital A, I suggest that you "limit" the application of these modifications to JUST that application (look at #IfWinActive). – Robert Ilbrink Mar 02 '14 at 17:36
  • Thanks Robert for your support, I edited my question and made a list. regards – eduardobedoya Mar 03 '14 at 19:41
  • Thanks Robert for your support, I think I need another kind of script that take in account which modifiers keys are been holding down while the "a" key is pressed, I edited my question and made a list as you suggested. Thanks Advanced. Regards – eduardobedoya Mar 04 '14 at 20:06
  • Would it be a solution to define a few regular keys like a, b, c, d, e, etc. to define your special key combinations and be able to toggle these keys on/off. Otherwise, are there any keys left that you would like to use. The last solution would be to add a second keyboard and to use HIDMacros (which can differentiate between keyboards) to define all your special keys on the second keyboard. – Robert Ilbrink Mar 08 '14 at 10:23
  • mmm Robert, I think I saw in some stackoverflow post, an Autohotkey script that launch actions only if a key combination is pressed in the keyboard, I mean an Autohotkey script that recognize wich modifiers keys are been pressed at given time?? do you know some of these scripts. Anyone please???? – eduardobedoya Mar 08 '14 at 20:17
  • Eduard, First of all, I get the impression that we are dealing the famous XY-problem (http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), so if you could go back to the real thing you are trying to achieve that would be great. Especially since you tried a loop, but could not stop it. On the other hand, continuing with your own solution, you could use the * in front of your hotkeys (ignore modifiers) and then inside your script check which modifiers were pressed at that time, then use send (or sendinput) and put the modifiers (which are ignored by send) back into the command. – Robert Ilbrink Mar 10 '14 at 07:19
  • Ok, RObert, thanks for your support, Its just that having two keyboard connected to a laptop is not seems to be a practical solution. The real thing I am trying to achieve is... I am looking for an Autohotkey script that recognize wich modifiers keys are been pressed at given time. Could you please write that last script you suggested, I really dont know how to type it. thanks man. – eduardobedoya Mar 11 '14 at 03:51
  • Eduard, I wrote some sample code where you can check in a binairy way which modifiers will cause what kind of behaviour. I hope this meets your requirements. If you have questions, just ask. – Robert Ilbrink Mar 11 '14 at 11:42
  • Thank you Robert, by running your script, if I keep the Ctrl key pressed and press A, I get a message box displaying LCtrl, keep the Ctrl key pressed and press A again and I get the same message box again, but the thrid time I press A I get no message box and when I release the Ctrl key and close the previous messages boxes I get a message "LCtrl AND LShift" pressed. I looks like this kind of script could be the solution, Its not necesary to differentiate between LShift, RShift, LCtrl, RCtrl, etc, just Ctrl, Shift, ALt, Win, and its combinations. Thanks you very much. – eduardobedoya Mar 12 '14 at 16:54
  • However when I replaced the MsgBox, LShift line with... send {LAlt down} send {d down} send {LAlt up} send {d up} In orther to get Alt + D whenever I press Shift + A without having to releasethe Shift key each time, I only get Alt + D the first time, the second time I hit A while keeping the Shift key pressed I get plain A instead. If I use this modified script in a text document it send only d not Shift D. Thanks Advanced. Note that it is not important how this script perform inside a text document, I mean I will use this script inside a editsoftware. Thannks RObert – eduardobedoya Mar 12 '14 at 17:13
  • Eduard, Sorry for the delays. I would suggest that you create a smooth migration plan, or even a situation where you have both programs running (using different keys of course). In AHK, You can do almost anything, so the question is how well can you describe the original thing that you (partially solved in HK4). There are macro recorders for some AHK versions, but I like to write the code myself. I use AutoHotkey v1.1.10.01 together with SciTE4AutoHotkey, which allows you to put in breakpoints, step by step, view variables, etc. So please describe what you want to do. – Robert Ilbrink Mar 29 '14 at 18:55
  • Thanks RObert, I use autohotkey L n Scite4AutoHotkey, I guess I should start with a mouse movements n clicks recorder, is there any macro recorder for autohotkey L? What I want to do in AHK is described above, basically... Setting variables that apply to every macro (like 200 macros) setting mouse movements, mouse clicks, copy selected text (number values) from floating windows inside an application, apply arithmetic operation to them and pasting the results inside the same boxes again all based on which variables are enable, also conditions to apply macros only inside some application windows – eduardobedoya Apr 01 '14 at 22:04
  • and some tittle windows, also inverted conditional, eg. apply one macro only inside some application, but not if some tittle windows is open. Also managing delays between keystrokes and mouseclicks. Could you make a template of that macro? Thanks Advanced. PD: do you suggest using AHK 1.1 over AHK L???? why? I would like to avoid discontinued software, I tent to use this script for long time. Thanks advanced Robert. – eduardobedoya Apr 05 '14 at 14:25
  • Eduard, as far as I know AHK_L is actively supported by Lexikos and by no means on the way out. You wrote about recording mouse movements and the need to run loops which you want to stop once the goal has been reached. I would suggest to keep the current HK4 and slowly add AHK scripts. Instead of mouse clicks I would try to use keyboard commands and controlSend (use AHK_Spy to hover over the objects you want to control to see their ID). This way your program always works. You can do a test inside a loop to verify a condition and if your goal has been reached use BREAK to exit the loop. – Robert Ilbrink Apr 14 '14 at 18:40
  • If you explain more about what you want to achieve (application and what you want to do), I could try to help you with your first steps. Currently I can't help you due to a lack of back ground knowledge. If you don't want to discuss this online you can contact me first.lastname on google mail. – Robert Ilbrink Apr 14 '14 at 18:44
  • Robert, pls tell me, perhaps the key command that you wrote dident work for me because I was using Autohotkey L?? I read that AHK1.1 scripts dont run properly in AHK L. Finally man, one little question, Are there on internet VIDEO or blog (with images) tutorials about autohotkey L, because I have search and I can find exactly what I need, could you suggest me some links? Thanks Advanced. – eduardobedoya Apr 16 '14 at 04:42
0

Just use a::A, save, and run. A is interpreted as Shift + a, and will work with key remapping. This will also work with #IfWinActive and #IfWinExist.

JMoore2007
  • 51
  • 4