Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@Aprillion

This comment has been minimized.

Copy link
Owner Author

Aprillion commented Apr 21, 2015

Usage

  • Download and install AutoHotkey
  • Download this file
  • Execute by double clicking
  • Stop via AutoHotkey icon in the system tray (letter H on green background) > right click > Exit

Screenshot

screenshot on twitter

@genmce

This comment has been minimized.

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

This comment has been minimized.

Copy link

MomentumV commented Mar 22, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.