Skip to content

Instantly share code, notes, and snippets.

@Aprillion
Forked from tmplinshi/KeypressOSD.ahk
Last active February 19, 2024 05:56
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aprillion/1016904b7573c32f00b9 to your computer and use it in GitHub Desktop.
Save Aprillion/1016904b7573c32f00b9 to your computer and use it in GitHub Desktop.
Display mouse and key presses on screen - above mouse pointer while typing (support multiple actions), then fade into top right corner
; KeypressOSD.ahk
;----------------------------------------------------------
; KeypressOSD_stacked v1.00
; Author : peter.hozak@gmail.com
; Date : 2015-04-21
; Tested on : Windows 8/AutoHotkey 1.1.19.03
; Thanks : KeypressOSD by tmplinshi - http://ahkscript.org/boards/viewtopic.php?f=6&t=225
; HotShow.ahk by RaptorX - http://www.autohotkey.com/board/topic/51641-hotshow-10-osd-hotkeys-for-video-tutorials/
; TODO: icons from https://openclipart.org/detail/216312/mouse-right-click
;----------------------------------------------------------
#SingleInstance force
#NoEnv
SetBatchLines, -1
ListLines, Off
CoordMode, Mouse, Screen
; Settings
transN := 60 ; 0=transparent, 255=opaque
ShowSingleKey := True ; display A-Z, Enter and other keys pressed without modifier (Ctr, Alt, ...)
DisplayTime := 1000 ; time to fade, in milliseconds
; Create GUI
Gui, +AlwaysOnTop -Caption +Owner +LastFound +E0x20
Gui, Margin, 0, 0
Gui, Color, Black
Gui, Font, cWhite s30 bold, Arial
Gui, Add, Text, vHotkeyText Center y50
WinSet, Transparent, %transN%
Winset, AlwaysOnTop, On
SetTimer, ShowHotkey, 100
; Create hotkey
Loop, 95
Hotkey, % "~*" Chr(A_Index + 31), Display
Loop, 24 ; F1-F24
Hotkey, % "~*F" A_Index, Display
Loop, 10 ; Numpad0 - Numpad9
Hotkey, % "~*Numpad" A_Index - 1, Display
Hotkey, ~*LButton, Display
Hotkey, ~*RButton, Display
Hotkey, ~*MButton, Display
Otherkeys := "NumpadDiv|NumpadMult|NumpadAdd|NumpadSub|Tab|Enter|Esc|BackSpace|Del|Insert|Home|End|PgUp|PgDn|Up|Down|Left|Right|ScrollLock|CapsLock|NumLock|Pause"
Loop, parse, Otherkeys, |
Hotkey, % "~*" A_LoopField, Display
return
; Display
;
PreviousKey := ""
Display:
If (A_ThisHotkey = "")
Return
mods := "Ctrl|Shift|Alt|LWin|RWin"
prefix := ""
Loop, Parse, mods, |
if GetKeyState(A_LoopField)
prefix .= A_LoopField "+"
if (!prefix && !ShowSingleKey)
return
key := PreviousKey prefix SubStr(A_ThisHotkey, 3)
PreviousKey := key " "
if (key = " ")
key := "Space"
LastHotkeyPressedTime := A_TickCount
Return
; Show Gui element with the hotkeys, move with mouse and fade into transparency
ShowHotkey:
prev_X := -999
prev_Y := -999
prev_LastHotkeyPressedTime := "999"
Gui, +LastFound
Loop {
Elapsed := A_TickCount - LastHotkeyPressedTime
Faded := 1 - Elapsed/DisplayTime
if (prev_LastHotkeyPressedTime != LastHotkeyPressedTime) {
WinSet, Transparent, % transN
} else if (Faded > 0.1) {
WinSet, Transparent, % transN * Faded
}
MouseGetPos, X, Y
if (prev_LastHotkeyPressedTime != LastHotkeyPressedTime or (((abs(prev_X - X) > 1 or abs(prev_Y - Y) > 1) or Faded < 0) and PreviousKey != "")) {
text_w := StrLen(key) * 20 + 50
if (Faded < 0.1) {
adjusted_X := A_ScreenWidth - text_w
adjusted_Y := 0
WinSet, Transparent, % 50
PreviousKey :=
} else {
adjusted_X := X - text_w / 2 - 5
adjusted_Y := Y - 55
}
GuiControl,, HotkeyText, %key%
GuiControl, Move, HotkeyText, +AlwaysOnTop w%text_w%
WinSet, Region, 0-0 W%text_w% H50 R50-50
Gui, Show, NoActivate x%adjusted_X% y%adjusted_Y% w%text_w%
prev_X := X
prev_Y := Y
prev_LastHotkeyPressedTime := LastHotkeyPressedTime
}
Sleep, 20
}
Return
@genmce
Copy link

genmce commented Sep 18, 2017

Is there a way to not have it fade to the top, just fade out?
Is there a way to just have it show "wheel up or down" once instead of multiples? I added wheelup/down to other

@MomentumV
Copy link

I get an error on line 36 that "* " is not a valid key name.

@javiavid
Copy link

I get the same error

@Aprillion
Copy link
Author

@javiavid I haven't used this script myself for several years, but https://stackoverflow.com/a/53208055/1176601 sounds related to the error message... (if you figure out what is the correct new syntax for line 36, I can update it for people who get here in the future)

@javiavid
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment