1

I want to remap alt+e when caps is on in autocad.

And when capslock is not on, alt+e should open menu edit.

I use script like this

<!e::
if(GetKeyState( "CAPSLOCK", "T" ))
{
    SendInput erase{space}wp{space}
}
else
{
    Send !e
}

When I turn on capslock, remap key is OK.

When I turn off capslockand alt+e, menu edit opened, but closed immediately.

Thanks.

coordinate
  • 15,224
  • 6
  • 24
  • 24
  • I want to post question at [autohotkey community](http://www.autohotkey.com/community). But it always give me error: You have provided an invalid answer to the question. – coordinate Jul 02 '12 at 07:38

2 Answers2

2

You will want a $ at the beginning of your hotkey to prevent the endless loop that the !e in your else block will trigger. You will also want to add a Return at the end of the hotkey to prevent the script from continuing into what is below this hotkey.

$!e::
if GetKeyState( "CapsLock", "T" )
    Sendinput, erase{space}wp{space}
else
    Sendinput, !e
Return

(Brackets are only required when if/else blocks are more than one line.)

Beyond that, the likely issue is that it's an alt hotkey that is also set to send alt.
I say this is an issue because if you press and hold alt, it activates menus,
and then the script sends alt, which will be in conflict with that.
As Ricardo said, the ideal way to script this is with the #IF command (only included with AHK_L).

#If GetKeyState("CapsLock", "T") and WinActive("AutoCAD")

!e:: SendInput, erase{space}wp{space}

#If

Notice that you can add the WinActive() function to the #If command's expression. Try it without that first, and also realize that the application's title needs to be exactly "AutoCAD" at all times for that to work. I would recommend finding AutoCad's ahk_class, with AHK's window spy, instead of using the title.

If it still does not work, it is likely that AHK is sending faster than AutoCAD would like to receive.
Info on how to deal with that can be found here.

Community
  • 1
  • 1
Honest Abe
  • 8,430
  • 4
  • 49
  • 64
  • Thanks Honest. Now I use shift+e instead of alt+e. I try your script and autohotkey_L. When caps is turn on, alt+e is not open menu edit, but input string "{ctrl}e" in autocad. – coordinate Jul 06 '12 at 06:42
1

Try to change your else block to this:

Send, {ALTDOWN}e{ALTUP}

I do not rely on these symbols to send keystrokes in AutoHotKey.

r_crispa
  • 54
  • 2
  • `#If GetKeyState( "CAPSLOCK", "T" ) <!e:: Send, erase{SPACE}wp{SPACE} return` – r_crispa Jul 03 '12 at 05:08
  • When I lanch "#If GetKeyState( "CAPSLOCK", "T" ) <!e:: Send, erase{SPACE}wp{SPACE} return". It error: Invalid hotkey – coordinate Jul 03 '12 at 07:17
  • 2
    I could not break lines in my last comment. The small code contains **four** lines, the _first_ is `#If GetKeyState ( "CAPSLOCK", "T" )`, the _second_ is `<!e::`, the _third_ is `Send, erase{SPACE}wp{SPACE}` and the _fourth_ and last is `return`. – r_crispa Jul 03 '12 at 11:39
  • Thanks. But I got error: LineText: #If....This line does not contain a recognized action. And I changed #If to If, this error is disappears. But another error Appeard to line erase.... – coordinate Jul 04 '12 at 01:40
  • Thanks. I updated to Autohotkey_l. The up code can be loaded. But keybounding ctrl+e can not switch by caps. And "#IfWinActive AutoCAD" did not work in autohotkey_l. – coordinate Jul 05 '12 at 07:00
  • Sorry, I just tested the hotkey Alt+e in a notepad window, I can't help you in the AutoCAD application. The code provided worked smoothly here. – r_crispa Jul 05 '12 at 19:51
  • Thanks Ricardo. I use shift+e instead of alt+e. It works fine now. – coordinate Jul 06 '12 at 06:44