# Macro
 This is how the macro system looks like
This page contains help on making a simple and powerful macro system in NetHack.

Contents

1 Installation
2 In-game usage
3 Sample macros
4 See Also
5 Links

Installation
This was tested on: WinXP SP3 + nethack-343-win (NetHackW.exe)

Download AutoHotkey Basic installer http://www.autohotkey.com/download/
Install AutoHotkey Basic
Create runmeonce.ahk AutoHotkey script containing following code in your NetHack directory:
;This is NethackW macro system script
;DO NOT edit unless you know what you're doing
#SingleInstance ignore

if A_IsCompiled &lt;&gt; 1
{ ;Create launcher file
	#NoTrayIcon
	Loop *.ico { ;Search for icon
		sIcon = %FileList%%A_LoopFileName%
		Break
	}
	if sIcon = 
	{
		MsgBox 16, Error, Icon is missing! Make sure if any file with .ico extension is present in Nethack directory.
		Run http://nethackwiki.com/wiki/Macro#icon
		ExitApp
	}
	RunWait Ahk2exe.exe /in "%A_ScriptFullPath%" /out "NHLauncher.exe" /icon "%sIcon%" /pass "nethack",Hide
	MsgBox 64, Information, Run NHLauncher.exe to start NetHack. Have fun!
	FileDelete %A_ScriptFullPath%
	FileDelete %sIcon%
	ExitApp
}

IfNotExist trigger.ahk
{
	MsgBox 16, Error, trigger.ahk not exist! You will be redirected to trigger.ahk source code.
	Run http://nethackwiki.com/wiki/Macro#trigger
	ExitApp
}
Process, Exist, NethackW.exe
NewPID = %ErrorLevel% 
if NewPID = 0
{ ;Start Nethack
	GoSub RunTrigger
	RunWait NethackW.exe
	GoSub KillTrigger	
	ExitApp
} else {
	MsgBox 16, Error, Nethack is already running. 
	ExitApp
}
return

KillTrigger:
	SetTitleMatchMode, 2
	DetectHiddenWindows, on
	SendMessage, 0x44, 0x405, 0, , trigger.ahk - AutoHotkey v
	RunWait TaskKill /pid %ErrorLevel% /fi "STATUS ne RUNNING",,Hide
return

RunTrigger:
	Run trigger.ahk
return

#ifWinActive ahk_class MSNHMainWndClass
!^r::
{ ;Reset triggers manually
	Gosub KillTrigger
	Gosub RunTrigger
	MsgBox 0, Information, Triggers script restarted
	return
}

^w::
{ ;NetHackWiki Search Engine
	InputBox, sWiki, Search on NetHackWiki, , , , 100
	if sWiki&nbsp;!=
		Run http://nethackwiki.com/index.php?search=%sWiki%&amp;fulltext=0
	return
}

^m::
{ ;Add new macro
	fTemp = trigger.tmp1
	bFound = 0
	bDelete = 0
	bSuccess = 0

	InputBox, sHotkey, Input trigger key,,, 200, 100
	if sHotkey =
	{ ;View triggers.ahk	
		Loop
		{
			FileReadLine, sLine, trigger.ahk, %A_Index%
			if ErrorLevel 
				break
			FileAppend, %sLine%`n, %fTemp%
			if ((sLine) = (";In game triggers"))
				fTemp = trigger.tmp2
		}
		FileRead Contents, trigger.tmp2
		if Contents =
			MsgBox 0, Information, Macro list is empty.
		else MsgBox 0, Information, Macro file contains following entries:`n`n%Contents%
		FileDelete trigger.tmp*
		Contents =  ; Free the memory.
		return	
	}
	InputBox, sAction, Input macro action,,, 200, 100
	if sAction =
		bDelete = 1
		
	Loop
	{ ;Read analize and save file
		FileReadLine, sLine, trigger.ahk, %A_Index%
		if ErrorLevel 
			break
		IfInString, sLine, %sHotkey%:: Send
		{ ;Equal entry found!
			bFound = 1
			if (bDelete &lt;&gt; 1)
			{
				MsgBox, 36, Old entry found!, %sLine%`n`nReplace with Send %sAction%?
				IfMsgBox Yes
				{
					FileAppend, %sHotkey%:: Send %sAction%`n, %fTemp%
				} else FileAppend, %sLine%`n, %fTemp%
				bSuccess = 1
			} else { ;Entry deletion
				MsgBox, 292, Entry deletion , Delete entry %sLine%?	
				IfMsgBox No
					FileAppend, %sLine%`n, %fTemp%					
				bSuccess = 1
			}
		} else FileAppend, %sLine%`n, %fTemp%
		if ((sLine) = (";In game triggers"))
			fTemp = trigger.tmp2
	}
	if (bFound = 0)
	{
		if (bDelete &lt;&gt; 1)
		{
			bSuccess = 1
			FileAppend %sHotkey%:: Send %sAction%`n, %fTemp%
		}
	}
	if (bSuccess &lt;&gt; 1)
		MsgBox 0, Error, Macro action cannot be empty
	{ ;Merge files and clear up
		FileRead Contents, trigger.tmp2
		Sort, Contents
		FileAppend %Contents%, trigger.tmp1
		FileCopy trigger.tmp1, trigger.ahk, 1
		FileDelete trigger.tmp*
		Contents =  ; Free the memory.
	}
	GoSub KillTrigger
	GoSub RunTrigger
}

Create trigger.ahk AutoHotkey script containing following code in your NetHack directory:
#NoTrayIcon
#SingleInstance ignore

MapScrollSteps = 28

#IfWinActive ahk_class MSNHMainWndClass
/:: 
	Loop 5 {
		Send s 
	}
	return

;Diagonal movement remapping
^a:: Send {Home}
^s:: Send {Pgup}
^z:: Send {End}
^x:: Send {Pgdn}

;Map scrolling left
^Left:: 
	Loop %MapScrollSteps% {
		Send ^{Left}
	}
	return

;Map scrolling right
^Right:: 
	Loop %MapScrollSteps% {
		Send ^{Right}
	}
	return

;WARNING! Leave one empty line at the end of file
;In game triggers
!Down:: Send o{Down}
!Left:: Send o{Left}
!Right:: Send o{Right}
!Up:: Send o{Up}
F12:: Send Oi{Enter}Oi{Enter}

Download custom .ico file and save it in your NetHack directory (for example: http://findicons.com/icon/257701/d12_128x128) Script will automatically use first .ico file in alphabetical order.
Run runmeonce.ahk to compile a script
In-game usage
Run NHLauncher.exe to start NetHack.
Press CTRL+M during the game and follow the on screen instructions to add a new macro action
Example: If you want to make a macro for casting your first spell (for instance to cast Extra healing on yourself)

press CTRL+M
Input trigger key: F1
Input macro action: Za.

Macro has been created and can be used immediately.

To view a full list of created macro triggers and actions press CTRL+M then ENTER
To delete a macro press CTRL+M next input it's trigger key and leave macro action empty. You will be asked to remove it.
Press CTRL+W and input a keyword. You will be redirected to appropriate NetHackWiki article.
Sample macros
There are already few macros in standard trigger.ahk file. Seaching macro can be removed only by editing a file so, if you don't need it you'll have to edit it by yourself.
Included macros:

Diagonal directions key mapping for laptops with uncomfortable position of [Home, End, Page Up, Page Down] keys changed to CTRL+[A, Z, S, X].
Searching for 5 turns by pressing / key.
F12 sorts inventory letters
ALT+[Direction] opens a door
CTRL+[Left, Right]Scrolls map smoothly. Change variable MapScrollSteps in trigger.ahk to adjust scroll range.
You can make your own macros during the game as well. Examples:

TAB key to apply an oil lamp.
F1,F2 to cast offensive spells.
F3,F4 to wear different sets of armor.
k for using a key/lockpick instead of kicking.
See Also
GNU&nbsp;screen does similar things on unixoid computers.
e. g. remapping the movement keys to avoid breaking pacifist conduct,
Paste
Links
List of special key aliases