I am posting further because you continue to try different suggestions and combinations and are not just asking for someone else to write your script.
I am going to copy/paste/modify several key definition examples in hopes of giving you a feel for different ways this can be done (in ways which may or may not have been mentioned already), and hopefully give you a better understanding of the various logic combinations that can be used instead of just solving your particular key combination problem. Hopefully you will be able to figure it out if I do not address it directly.
Example: Trigger only if Space is pressed.
#If GetKeyState("Space", "P") ; All hotkeys below won't trigger unless space is pressed
q::
ToolTip % "Space+q pressed..."
Sleep 500
ToolTip
Return
!q::
ToolTip % "Space+Alt+q pressed..."
Sleep 500
ToolTip
Return
#If
Example: Inspecific triggers for multiple modifier checks within the hotkey code.
#If WinActive("ahk_class xxx") || WinActive("ahk_class YYY")
*q:: ; * Triggers for all/multiple modifiers. q keypress is discarded.
If !(GetKeyState("Alt", "P") && GetKeyState("Space", "P"))
Return ; continue no further unless Alt+Space are also pressed. q keypress is discarded
; This code below will execute for Alt+Space-q and also Alt+Space+Control+q (i.e., * modifiers that haven't been excluded via test above)
ToolTip % A_ThisHotkey " pressed..."
Sleep 500
ToolTip
Return
Example: Manually send the keystroke (i.e., in-hotkey modifier filtering)
#If WinActive("ahk_class xxx") || WinActive("ahk_class YYY")
*q:: ; * Triggers for all/multiple modifiers
If !(GetKeyState("Alt", "P") && GetKeyState("Space", "P")) {
SendInput {Blind}q ; send q keystroke and allow any pressed modifiers to stay in tact. If Control is down, Control+q will get sent
Return ; continue no further unless Alt+Space are also pressed.
}
; This code below will execute for Alt+Space-q and also Alt+Space+Control+q (i.e., * modifiers that haven't been excluded via test above)
ToolTip % A_ThisHotkey " pressed..."
Sleep 500
ToolTip
Return
Example: Keystroke gets sent to program regardless if you act on it or not.
#If WinActive("ahk_class xxx") || WinActive("ahk_class YYY")
~*q:: ; * Triggers for all/multiple modifiers; ~ allow passthrough so keystroke is not discarded
If !(GetKeyState("Alt", "P") && GetKeyState("Space", "P"))
Return ; continue no further unless Alt+Space are also pressed. q keypress has already passed through to program
; This code below will execute for Alt+Space-q and also Alt+Space+Control+q (i.e., * modifiers that haven't been excluded via test above)
ToolTip % A_ThisHotkey " pressed..."
Sleep 500
ToolTip
Return
Example: Multiple definitions/actions in a single hotkey (works but not recommended implementation).
#If WinActive("ahk_class xxx") || WinActive("ahk_class YYY")
*q:: ; * Triggers for all/multiple modifiers; ~ allow passthrough so keystroke is not discarded
If !GetKeyState("Space", "P")
Return ; continue no further unless Alt+Space are also pressed. q keypress is discarded
If (GetKeyState("Alt", "P") {
ToolTip % "Alt+Space+q (and possibly other modifiers) pressed..."
Sleep 500
ToolTip
} Else {
ToolTip % "Space+q (and possibly other modifiers) pressed..."
Sleep 500
ToolTip
}
Return
You can differentiate what keys are down inside of the hotkey definition and selectively execute distinct sets code, but general best practice is probably to use #If
statements to determine when the hotkey code executes.
Don't use two #If's
in a row... the hotkey definition is determined by the last sequential #If
statement before the hotkey definition statement...
; Do Not Use...
#If WinActive("ahk_class xxx") || WinActive("ahk_class YYY")
#If (GetKeyState("Alt", "P")) && (GetKeyState("Space", "P")) ; this voids the #If statement above
Should be as follows below.
; If ahk_class xxx or yyy, and if Alt+Space is also pressed...
#If (WinActive("ahk_class xxx") || WinActive("ahk_class YYY")) && (GetKeyState("Alt", "P") && GetKeyState("Space", "P"))
; ...
; code goes here
; ...
#IfWinActive ; this sets context for the next hotkeys back to default; only one statement needed; indentation is irrelevant
Note the use of grouping parens as needed for logical OR/AND combinations... you can get away without them in some cases but be careful to combine the statements carefully when mixing ANDs and ORs.
This statement can also be continued automatically on two lines (for style/readability) since the boolean operators are recognized as continuing the last line...
#If (WinActive("ahk_class xxx") || WinActive("ahk_class YYY"))
&& (GetKeyState("Alt", "P") && GetKeyState("Space", "P"))
;q:: ; q hotkey without modifiers will never trigger here because Alt must be down to get this far
!q:: ; this will trigger
ToolTip % "Space+Alt+q pressed..."
Sleep 500
ToolTip
Return
!d::
ToolTip % "Space+Alt+d pressed..."
Sleep 500
ToolTip
Return
#If
Example: Split processing (inside/outside hotkey definition) (works but not recommended implementation)
#If (WinActive("ahk_class xxx") || WinActive("ahk_class YYY"))
!q::
If GetKeyState("Space", "P") {
ToolTip % "Space+Alt+q pressed..."
Sleep 500
ToolTip
} Else {
ToolTip % "Alt+q pressed..."
Sleep 500
ToolTip
}
Return
#If
Example: All processing inside hotkey definition (works but not recommended implementation)
!q::
If !(WinActive("ahk_class xxx") || WinActive("ahk_class YYY"))
Return
If GetKeyState("Space", "P") {
ToolTip % "Space+Alt+q pressed..."
Sleep 500
ToolTip
} Else {
ToolTip % "Alt+q pressed..."
Sleep 500
ToolTip
}
Return
#If
Final example with code split between hotkeys as needed and #If
statements down-selecting to always have a certain class window active and Space pressed...
#Persistent
Return
#If (WinActive("ahk_class xxx") || WinActive("ahk_class YYY"))
&& GetKeyState("Space", "P") ; All hotkeys below won't trigger unless space is pressed
;!Space::Return ; Optional. Kill the window system menu if present
q::
ToolTip % "Space+q pressed..."
Sleep 500
ToolTip
Return
!q::
ToolTip % "Space+Alt+q pressed..."
Sleep 500
ToolTip
Return
d::
ToolTip % "Space+d pressed..."
Sleep 500
ToolTip
Return
!d::
ToolTip % "Space+Alt+d pressed..."
Sleep 500
ToolTip
Return
#If
Note that this does not void the use of Space
or Alt+Space
as standalone keystrokes. For example if you press Space
and then Alt
and then a hotkey, it will send a Space
before it executes the Alt+hotkey
code. Similarly, if you press Alt+Space
(say in a normal window) it will trigger the system window in the top left (Restore/Move/Size/Minimize/Maximize/Close). The hotkey functionality will still work, but the menu dropping down might be an annoyance (if you're not in a tool window that excludes this menu already), in which case you can also add an !Space::Return
function to kill the functionality of Alt+Space
while retaining the hotkey. In that case it should be added within the context block (relevant #If
statement) so it only applies to that particular window.
There are multiple other ways to get code to execute for these key combinations as well, without using GetKeyState() (as noted in some of the other answers)--I am not going to reduplicate that information. You should be able to get some combination of answers posted to do what you want.
Best Answer
Try this:
I know you technically want it to hit alt then f, but this has the same effect.