AHK inside Autocad

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
wilmath
Posts: 3
Joined: 28 Apr 2022, 18:58

AHK inside Autocad

Post by wilmath » 22 Jun 2022, 19:22

I am new to AHK and want to create hot keys to use while I am using the Autocad program.
There is a command alias program within autocad but it only works with ordinary commands
and won't work with transparent commands. (Transparent commands are mostly used with
object snaps such as endpoint or intersection and happen during a command.)

I know I have to let AHK know if Autocad is open but I don't know the exact
format and if I need to include the full path to autocad.

Thanks in advance
Rick

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: AHK inside Autocad

Post by boiler » 22 Jun 2022, 21:48

You posted in the v2 section of "Ask For Help". Are you sure you're using AHK version 2, which is in beta release? Most new forum members and new AHK users who post here are really using v1, which is still the official release (1.1.34.03 is the latest). If you don't know, run the script below in whichever version you have, and it will tell you which major version you're using.

Code: Select all

v1 := "v2"
MsgBox v1

CptRootBeard
Posts: 26
Joined: 16 Nov 2020, 14:47
Contact:

Re: AHK inside Autocad

Post by CptRootBeard » 23 Jun 2022, 15:40

I've been using AHK (v2, but v1 would work as well) with AutoCAD for a while now with a good amount of success. What are you trying to do in particular?

If you just want to set up hotkeys, you'll need the #HotIf directive (#If in v1). Here's a trivial example:

Code: Select all

;Assuming v2 beta for this example
#Requires AutoHotkey v2.0-beta

;;You'll just need the name of the AutoCAD process (acad.exe)
#HotIf WinActive("ahk_exe acad.exe")

^!d::{
	;F3 is usually a default for toggling OSnaps on and off
	SendInput("{F3}")
	tooltip("You pressed ctrl-alt-d!")
	sleep 600
	tooltip()
}
I've personally had mixed results with external hotkeys in CAD, because its built-in command line would sometimes 'steal' the key presses before they got to AHK.
If you're looking for something along these lines, I'd really recommend using AutoCAD's extensive CUI/customization system.

If you want to use AHK to work with entities in the AutoCAD drawing (similar to AutoLISP or VBA in CAD), you'll want to use the AutoCAD Application COMObject.
I've had a lot of luck using custom LISP commands to call compiled AHK programs with this method.

wilmath
Posts: 3
Joined: 28 Apr 2022, 18:58

Re: AHK inside Autocad

Post by wilmath » 24 Jun 2022, 00:32

@boiler, I ran the script and I have v1.
[Mod action: Thread moved to v1 section]

cptRootBeard, I know about the cui/customization, I have been using the pgp file for years and also autolisp. However, neither of these
will work for point filters when I am in the middle of a command and also object snaps. I know it is possible to turn on and turn off snaps but I would
rather tell the program that I just want end snaps or intersection snaps, or whatever. I don't like the cursor to bounce around the screen like a ping pong
ball when I am trying to zero in on a point. And when I want to tell the program where the x coordinate or y coordinate is in the middle of the program
autocads command alias's won't work.

When I first started autocad many moons ago, a person could use Dos to do this. I would use either alt or ctrl as a prefix and then the key of whatever I was trying to do.
.x which is a point filter transparent command became alt x. It worked great because I didn't have to take my left hand off the keyboard and I didn't have to
wander around the screen looking for the .x point filter icon.

There was a third party program that worked with Autocad that did just that but they vanished and another one called "auto hook" came later. I would still be
using Autohook but the guy that created it died and the son is not supporting it.

I don't know if ahk will work for what I am trying to do but I want to give it a shot. Both windows and autocad use alt as a shortcut key so that key may not
work but if I could use the ctrl key, especially if I was able to remap it to a mouse button, that would be just the ticket.

Thanks guys for your help!

CptRootBeard
Posts: 26
Joined: 16 Nov 2020, 14:47
Contact:

Re: AHK inside Autocad

Post by CptRootBeard » 24 Jun 2022, 13:47

I'm still convinced the CUI will be the way to go for you, but I'll present a few options here and you can try what works best:

First, using AHK (v1, both examples shown in the script below):
1. Send OSMODE override values. The nice thing about this is it will work inside OR outside of other commands. Your selected snap settings will hang around after, though.
2. Send pre-existing object snap macros. These won't require any other configuration. See CUI>Shortcut Menus>Command Menu>Snap Overrides for a complete listing. These won't stick around after the command is done.

Code: Select all

#Requires AutoHotkey v1.1.33+

;;You'll just need the name of the AutoCAD process (acad.exe)
#IfWinActive ahk_exe acad.exe

;The tooltip/sleep calls are just to give you visual feedback while testing.
;I'd remove them from any actual code you end up using.
F3::
	;OSMODE = 32 is intersection
	;Using the single quote tells the macro we're using a command
	Send % "'osmode{space}32{space}"
	tooltip % "You pressed F3"
	sleep 600
	tooltip
return

^m::
	;_mid is midpoint snapping
	;Notice this is a macro itself, so does NOT require the leading single quote
	Send % "_mid{space}"
	tooltip % "You pressed ctrl+m"
	sleep 600
	tooltip
return
Second, using only the CUI file:
1. Add a new shortcut entry to CUI>Keyboard Shortcuts>Shortcut Keys, and use the following macro format:

Code: Select all

^P'_.osmode 32
(where 32 is your desired OSMODE value)
2. Add a new shortcut entry to CUI>Keyboard Shortcuts>Shortcut Keys, and use the macros from method 2 above.
3. Use Temporary Override Keys to set the OSMODE value. See CUI>Keyboard Shortcuts>Temporary Override Keys and look at the Key Down macros for any of the Object Snap Override entries.

Both method 1's and 2's are pretty much equivalent. If you have better luck with any one route over another, or no luck at all, please let me know.
You also have the option of the Snap Override context menu with Shift+RClick, but I'm assuming you want to avoid using a visual menu.

wilmath
Posts: 3
Joined: 28 Apr 2022, 18:58

Re: AHK inside Autocad

Post by wilmath » 06 Jul 2022, 01:05

I was going to try creating hot keys within Autocad but couldn't figure it out. Command alias's are relatively
straight forward but I didn't see a way to do transparent commands.

I came up with a script for Ahk but have a question. When I created the file there seemed to be code
already in it. Is this something I need to deal with?


Below is a paste of that file and what I have so far.
Thanks

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.

#IfWinActive ahk_exe acad.exe

!e::
Send, end
return

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: AHK inside Autocad

Post by boiler » 06 Jul 2022, 02:21

You don’t necessarily need those four lines that are created when you create a new AHK file in FileExplorer. I usually delete them. For your script, you might want to keep the SendMode line. Try your script without it, and you’ll see the Send command will send characters slower, although you’re not sending many characters anyway.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: AHK inside Autocad

Post by BoBo » 06 Jul 2022, 07:03

@wilmath - AFAIK, you can edit the "template.ahk" file that is setting these entries to your liking.
Check out its default location: C:/Windows/Shellnew. HTH

BTW. viewtopic.php?f=17&t=9528

Post Reply

Return to “Ask for Help (v1)”