Skip to content

Instantly share code, notes, and snippets.

@rfmarves
Forked from tmplinshi/KeypressOSD.ahk
Last active October 30, 2016 23:42
Show Gist options
  • Save rfmarves/c650ef9a4ce6757a1a953ddb791b0308 to your computer and use it in GitHub Desktop.
Save rfmarves/c650ef9a4ce6757a1a953ddb791b0308 to your computer and use it in GitHub Desktop.
; KeypressOSD.ahk
;----------------------------------------------------------
; ChangeLog : v2.06 (2016-10-31) - Added options to Hide Unmodified mouse keys and sindle alphabet keys.
; - Added GuiMargin option to adjust Gui placement
; - Changed Text spacing logic for more accurate vertical centering
; - Added option for translation replacement.
; v2.05 (2016-10-01) - Fixed not detecting "Ctrl + ScrollLock/NumLock/Pause"
; v2.04 (2016-10-01) - Added NumpadDot and AppsKey
; v2.03 (2016-09-17) - Added displaying "Double-Click" of the left mouse button.
; v2.02 (2016-09-16) - Added displaying mouse button, and 3 settings (ShowMouseButton, FontSize, GuiHeight)
; v2.01 (2016-09-11) - Display non english keyboard layout characters when combine with modifer keys.
; v2.00 (2016-09-01) - Removed the "Fade out" effect because of its buggy.
; - Added support for non english keyboard layout.
; - Added GuiPosition setting.
; v1.00 (2013-10-11) - First release.
;----------------------------------------------------------
#SingleInstance force
#NoEnv
SetBatchLines, -1
ListLines, Off
; Settings
global TransN := 127 ; 0~255
global ShowSingleKey := True ; True or False
global ShowOnlySpecialKeys := True ; True or False. ShowSingleKey has to be set to False for this to work.
global ShowMouseButton := True ; True or False
global ShowOnlyModMouseButton := True ; True or False. ShowMouseButton needs to be set to True for this to work.
global DisplayTime := 2000 ; In milliseconds
global GuiPosition := "Bottom" ; Top or Bottom
global FontSize := 50
global GuiHeight := 115
global GuiMargin := 50 ; This is the distance from the Gui window to the bottom or top of the ActWindow
global KeyLanguage := "en" ; Select key language here. "en" for English, "es" for Spanish. Ctrl+Alt has been re-displayed as AltGr in the Spanish Keyboard.
;Enter language translations here
;Current know bug in tranlation: If there is a string replacement that is contained in the replacement text, the replacements will cicle.
;eg: "Up" gets replaced by "Arriba". Del gets replaced by "Supr". Since "Supr" contains an "up", the result is "SArribar".
global KeyTrans_es := { LWin: "Win", RWin:"Win", Space:"Espacio", LButton:"ClickIzq", RButton:"ClickDer", MButton:"ClickMedio", Up:"↑", Down:"↓", Right:"→", Left:"←", CapsLock:"BloqMayús", NumLock:"BloqNum", Pause:"Pausa", AppsKey:"TeclaApps", NumpadDot:".", NumpadSub:"-", NumpadEnter:"Enter", BackSpace:"Retroceso", PgDn:"Pg↓", Del:"Spr", NumpadDiv:"/", NumpadMult:"*", NumpadAdd:"+", "Ctrl + Alt":"AltGr"}
CreateGUI()
CreateHotkey()
return
OnKeyPressed:
try {
key := GetKeyStr()
ShowHotkey(key)
SetTimer, HideGUI, % -1 * DisplayTime
}
return
; ===================================================================================
CreateGUI() {
global
Gui, +AlwaysOnTop -Caption +Owner +LastFound +E0x20
Gui, Margin, 0, 0
Gui, Color, Black
Gui, Font, cWhite s%FontSize% bold, Arial
yText := ((GuiHeight - FontSize ) /2)
Gui, Add, Text, vHotkeyText Center y%yText%
WinSet, Transparent, %TransN%
}
CreateHotkey() {
Loop, 95
Hotkey, % "~*" Chr(A_Index + 31), OnKeyPressed
Loop, 24 ; F1-F24
Hotkey, % "~*F" A_Index, OnKeyPressed
if !(ShowOnlySpecialKeys) {
Loop, 10 ; Numpad0 - Numpad9
Hotkey, % "~*Numpad" A_Index - 1, OnKeyPressed
}
Otherkeys := "AppsKey|NumpadDot|NumpadDiv|NumpadMult|NumpadAdd|NumpadSub|NumpadEnter|Tab|Enter|Esc|BackSpace"
. "|Del|Insert|Home|End|PgUp|PgDn|Up|Down|Left|Right|ScrollLock|CapsLock|NumLock|Pause|sc145|sc146|sc046"
Loop, parse, Otherkeys, |
Hotkey, % "~*" A_LoopField, OnKeyPressed
If ShowMouseButton {
Loop, Parse, % "LButton|MButton|RButton", |
Hotkey, % "~*" A_LoopField, OnKeyPressed
}
}
ShowHotkey(HotkeyStr) {
WinGetPos, ActWin_X, ActWin_Y, ActWin_W, ActWin_H, A
if !ActWin_W
throw
text_w := ActWin_W
GuiControl, , HotkeyText, %HotkeyStr%
GuiControl, Move, HotkeyText, w%text_w% Center
if (GuiPosition = "Top")
gui_y := ActWin_Y + GuiMargin
else
gui_y := (ActWin_Y+ActWin_H) - GuiHeight - GuiMargin
Gui, Show, NoActivate x%ActWin_X% y%gui_y% h%GuiHeight% w%text_w%
}
GetKeyStr() {
static modifiers := ["Ctrl", "Shift", "Alt", "LWin", "RWin"]
for i, mod in modifiers {
if GetKeyState(mod)
prefix .= mod " + "
}
if (!prefix && !ShowSingleKey)
throw
key := SubStr(A_ThisHotkey, 3)
if (key = " ") {
if (prefix or !ShowOnlySpecialKeys) {
key := "Space"
} else
throw
} else if ( StrLen(key) = 1 ) {
if (prefix or !ShowOnlySpecialKeys) {
key := GetKeyChar(key, "A")
} else
throw
} else if ( SubStr(key, 1, 2) = "sc" ) {
key := SpecialSC(key)
} else if ((key = "LButton" or key = "MButton" or key = "RButton") && !prefix && ShowOnlyModMouseButton ) {
throw
} else if (key = "LButton") && IsDoubleClick() {
key := "Double-Click"
}
if !(KeyLanguage = "en") {
For key_item, new_name in KeyTrans_%KeyLanguage%
StringReplace, prefix, prefix, %key_item%, %new_name%, All
For key_item, new_name in KeyTrans_%KeyLanguage%
StringReplace, key, key, %key_item%, %new_name%, All
}
return prefix . key
}
SpecialSC(sc) {
static k := {sc046: "ScrollLock", sc145: "NumLock", sc146: "Pause"}
return k[sc]
}
; by Lexikos -- https://autohotkey.com/board/topic/110808-getkeyname-for-other-languages/#entry682236
GetKeyChar(Key, WinTitle:=0) {
thread := WinTitle=0 ? 0
: DllCall("GetWindowThreadProcessId", "ptr", WinExist(WinTitle), "ptr", 0)
hkl := DllCall("GetKeyboardLayout", "uint", thread, "ptr")
vk := GetKeyVK(Key), sc := GetKeySC(Key)
VarSetCapacity(state, 256, 0)
VarSetCapacity(char, 4, 0)
n := DllCall("ToUnicodeEx", "uint", vk, "uint", sc
, "ptr", &state, "ptr", &char, "int", 2, "uint", 0, "ptr", hkl)
return StrGet(&char, n, "utf-16")
}
IsDoubleClick(MSec = 300) {
Return (A_ThisHotKey = A_PriorHotKey) && (A_TimeSincePriorHotkey < MSec)
}
HideGUI() {
Gui, Hide
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment