[Script] KeypressOSD - Display key press on screen

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: [Script] KeypressOSD - Display key press on screen

16 Jan 2018, 10:01

The colors fix should now yield a fixed six character string for any selected color so you may wanna go through the variable checks and optimize StrLen(var) checks from less than/more than 6 to StrLen(var) != 6.
And put back those darn custom colors, let the user be happy - it only costs a few milliseconds to read from/write to ini, once per session.
Part of my AHK work can be found here.
robodesign
Posts: 934
Joined: 30 Sep 2017, 03:59
Location: Romania
Contact:

Re: [Script] KeypressOSD - Display key press on screen

16 Jan 2018, 15:52

Hello!

Today's new version is here:

Download AHK file from or use the Update now option:
https://github.com/marius-sucan/KeyPress-OSD
http://marius.sucan.ro/media/files/blog ... ss-osd.ahk
Please use AHK_H v1.1.27 [unicode] to execute this script.

Change logs:

v3.99 (2018-01-16)
- reduced [by about 7 kilobytes] and optimized code for handling modifiers [Ctrl, Shift, AltGr and so on]
- as a consequence, I removed the option for Sticky Keys
- [bug fixed] the bug with Shift + VKdc reported by burque505
- bug fixes from drugwash regarding setColors()
......and various other fixes...

v3.98.7 (2018-01-15)
- [bug fixed] the "too many fonts" error is fixed now. AHK is limited to 200 fonts, but using Font Library, I managed to circumvent this problem. This error apeared at Preferences when changing/previewing many fonts.

Thank you both very much for the feedback and help. Next is to.... somehow make it set itself the right kbd layout when using the Alternate typing mode.

Please let me know if you find any regressions. The changes were quite substantial... affecting Shift/AltGr.

PS. Just noticed there's a new AHK version addressing issues with dead keys. Tomorrow I will test it and see how/what impacts regarding my script.

Best regards, Marius.
-------------------------
KeyPress OSD v4: GitHub or forum. (presentation video)
Quick Picto Viewer: GitHub or forum.
AHK GDI+ expanded / compilation library (on GitHub)
My home page.
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: [Script] KeypressOSD - Display key press on screen

16 Jan 2018, 19:33

Hi Marius,
I did a fairly quick check - I didn't find any issues at all this time. All the dead keys appear to be working whether Ctrl + Ins is pressed or not.
I am using the new AHK_H (1.1.27.06), Win7, 64-bit.
Kudos! :bravo:
Regards,
burque505
robodesign
Posts: 934
Joined: 30 Sep 2017, 03:59
Location: Romania
Contact:

Re: [Script] KeypressOSD - Display key press on screen

17 Jan 2018, 03:44

OMG, that IS great to hear!!!!

I will test it today as well. Thank you, burque505.

@drugwash: I just tested color editing and it is not working. Last evening I was in a hurry. When I click on the color to edit it, it's always starting from black.

Best regards, Marius.
-------------------------
KeyPress OSD v4: GitHub or forum. (presentation video)
Quick Picto Viewer: GitHub or forum.
AHK GDI+ expanded / compilation library (on GitHub)
My home page.
User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: [Script] KeypressOSD - Display key press on screen

17 Jan 2018, 09:44

Indeed, I had failed to check properly after modifying the functions. Use this line in Dlg_Color() instead of the original:
Color := "0x" hexRGB(InStr(Color, "0x") ? Color : Color ? "0x" Color : 0x0)
(note the added "0x" before the hexRGB() call, that should fix it)

You still didn't fix this in mouse-functions (they're both pointers, not UInt):
DllCall("GetClientRect", "Ptr", MyGuiHWND := WinExist(), "Ptr", &rect )

Down in the ; verify HEX values section you have hastily modified two values that shouldn't have been modified, because those are not colors to have a string length of six, but of eight: forcedKBDlayout1 and forcedKBDlayout2.
Part of my AHK work can be found here.
robodesign
Posts: 934
Joined: 30 Sep 2017, 03:59
Location: Romania
Contact:

Re: [Script] KeypressOSD - Display key press on screen

17 Jan 2018, 12:18

Hello!

Okydoky, changed those. THANK YOU VERY MUCH, drugwash. Yes, I admit. Stupid/silly errors slip my fingers often.

Download AHK file from or use the Update now option:
https://github.com/marius-sucan/KeyPress-OSD
http://marius.sucan.ro/media/files/blog ... ss-osd.ahk
Please use AHK_H v1.1.27 [unicode] to execute this script.

Notable changes:
v3.99.1 (2018-01-17)
- {fixed} regressions related to usage with sticky keys enabled in Windows
- {fixed} changing colors at preferences
- {fixed} unwanted shortcuts getting triggered when not needed on foreign kbd layouts [reffering to Ctrl+A/C/V/X/Z]

------
With the new AHK, the script runs better ;-). Do you need me to upload the new AHK? @drugwash

@drugwash: I am still struggling with changing the kbd layout to my own window... Your indicated solution does not seem the right one: you force load a new kbd layout... or am I wrong?

Best regards, Marius.
-------------------------
KeyPress OSD v4: GitHub or forum. (presentation video)
Quick Picto Viewer: GitHub or forum.
AHK GDI+ expanded / compilation library (on GitHub)
My home page.
User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: [Script] KeypressOSD - Display key press on screen

17 Jan 2018, 15:01

Errare humanum est (, sed perseverare diabolicum). ;)
I borrowed a new modem from a friend so now I can download the large files but thank you for the offer.

The keyboard script will load a new layout only if it wasn't previously loaded, which in theory should never happen since we retrieve the layout from the active window/control, which already has it loaded. Have you tested it by itself? Try this:

Code: Select all

gui, +alwaysontop
gui, add, edit, w400 h300,
gui, add, text, w400 h16,
gui, show, NoActivate,
settimer, chk, 500
return
chk:
threadID := GetFocusedThread(hwnd := WinExist("A"))
hkl := DllCall("GetKeyboardLayout", "UInt", threadID)				; 0 for current thread
SetFormat, Integer, H
hkl+=0
SetFormat, Integer, D
if !DllCall("ActivateKeyboardLayout", "UInt", hkl, "UInt", 0x100)	; hkl: 1=next, 0=previous | flags: 0x100=KLF_SETFORPROCESS
	{
	SetFormat, IntegerFast, H
	l := SubStr(hkl & 0xFFFF, 3), klid := SubStr("00000000" l, -7)
	SetFormat, IntegerFast, D
	DllCall("LoadKeyboardLayout", "Str", klid, "UInt", 0x103)		; AW, flags: 0x100=KLF_SETFORPROCESS 0x1=KLF_ACTIVATE 0x2=KLF_SUBSTITUTE_OK
	}
VarSetCapacity(klid, 9*2, 0)	; 9 Unicode chars
DllCall("GetKeyboardLayoutName", "Str", klid)	; AW
guicontrol,, Static1, hwndA=%hwnd% hkl=%hkl% klid=%klid%
return

GetFocusedThread(hwnd := 0)
{
if !hwnd
	return 0	; current thread
tid := DllCall("GetWindowThreadProcessId", "Ptr", hwnd, "Ptr", NULL)
VarSetCapacity(GTI, sz := 24+6*A_PtrSize, 0)			; GUITHREADINFO struct
NumPut(sz, GTI, 0, "UInt")	; cbSize
if DllCall("GetGUIThreadInfo", "UInt", tid, "Ptr", &GTI)
	if hF := NumGet(GTI, 8+A_PtrSize, "Ptr")
		return DllCall("GetWindowThreadProcessId", "Ptr", hF, "Ptr", NULL)
return 0	; current thread (actually it's an error but we couldn't care less)
}
As far as I understand, the abomination always sets the layout overall for all applications and apps (this happens starting with Win8, actually, according to MSDN), so this piece of script would only be useful in Win7 and lower, where layouts can be set per-appplication.
Part of my AHK work can be found here.
robodesign
Posts: 934
Joined: 30 Sep 2017, 03:59
Location: Romania
Contact:

Re: [Script] KeypressOSD - Display key press on screen

17 Jan 2018, 15:53

Thank you very much, drugwash. Yes, for Win 7 and lower I need it. I will try it tomorrow and see if it works. And even, in Win10, you can activate layouts per apps...

Best regards, Marius.
-------------------------
KeyPress OSD v4: GitHub or forum. (presentation video)
Quick Picto Viewer: GitHub or forum.
AHK GDI+ expanded / compilation library (on GitHub)
My home page.
User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: [Script] KeypressOSD - Display key press on screen

17 Jan 2018, 16:55

The test worked here on XP, I quickly installed a couple extra layouts and the script layout got automatically changed when switching active window where windows had different layouts. You just need a reliable active window switch detection, preferrably based on WM_* (windows messages), not on timer (I hate timers) and not only on mouse move since user may cycle through Alt-Tab or windows could be spawned/closed by current processes.
Good luck! I'm off to bed with a bad headache.
Part of my AHK work can be found here.
robodesign
Posts: 934
Joined: 30 Sep 2017, 03:59
Location: Romania
Contact:

Re: [Script] KeypressOSD - Display key press on screen

17 Jan 2018, 17:14

Okydoky, thank you very much. I will keep in mind. And, get well.
-------------------------
KeyPress OSD v4: GitHub or forum. (presentation video)
Quick Picto Viewer: GitHub or forum.
AHK GDI+ expanded / compilation library (on GitHub)
My home page.
User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: [Script] KeypressOSD - Display key press on screen

18 Jan 2018, 07:59

Thanks, I'm a little better today.

Small optimizations:

Code: Select all

capsHighlightDummy() {
    GetKeyState, CapsState, CapsLock, T
    GuiControl, OSD:, CapsDummy, % (CapsState = "D") ? 100 : 0
    SetTimer,, off
}
In GuiGetSize() the Round function is useless; once you specifically ask NumGet() to retrieve UInt the values will always be integers even if Float format would be in effect, so there would be nothing to round. So just do:

Code: Select all

  W := NumGet(rect, 8, "UInt")
  H := NumGet(rect, 12, "UInt")
I still had the issue of 'Too many fonts' with my mod although I merged those font functions and the respective calls to them. Adding a Sleep, 10 after Gui, OSD:Destroy in CreateOSDGUI() seems to have fixed it but further testing is needed.
Also had to change a line to:
if (prefOpen=0 || showPreview=0)
because I'm allowing for showPreview to be always on through the ini settings and that if/else broke things, font name/size never got to be applied to the OSD in normal usage - worked only in preview.

Speaking of fonts, for some of them width is not correctly calculated, cutting the text short (i.e. Android Emoji, Marlett, Monotype Sorts, MV Boli, Noto Emoji, Open Sans Extrabold, Open Sans Semibold, Segoe UI Semibold, Webdings, Wingdings/2/3) or leaving too much room behind (i.e. Courier, Fixedsys, Modern, MT Extra, Roman, Script, Small Fonts, System, Terminal, WST_*), while for other symbol fonts it's all right (i.e. kinjaicons).
Also the height of the Caps/Alt.mode Progress seems to be fixed and certain fonts exceed that height (i.e. BurigangaKamalaGMJ, Cambria Math, Gautami, Javanese Text, Mangal, Myanmar Text, Raavi, Segoe Print, Segoe Script, Tunga, Vani).
A special case is Counter-Dial, it doesn't render correctly in OSD, but that font is a system killer so better let it be.
I know what you said about unusual fonts and all that but there's no telling what the user needs this script for so it better match their preferences or they'll dump it.
And the OSD caret is still displaying as an empty square with certain fonts (i.e. Tahoma) so maybe you could try some other more common character or use another approach (like insert a space, calculate its position and display something else on top of it - like a progress, a mini-GUI or whatever). Considering the user may use any font at any given moment the caret should be unique and visible for all fonts.

There's one thing not quite right in my opinion: mouse halo and idle halo appear on top of all fullscreen applications. That includes video players and you can imagine a flashing circle being annoying in such situation. Therefore I believe the script should implement a list of process exclusions and a fullscreen check in order to automatically disable either halo per user preference (for example user may want to keep position halo but disable idle halo in some application, and the other way around in some other application).
Part of my AHK work can be found here.
robodesign
Posts: 934
Joined: 30 Sep 2017, 03:59
Location: Romania
Contact:

Re: [Script] KeypressOSD - Display key press on screen

18 Jan 2018, 08:43

Okay, I will soon post a new version. Made good progress on it.

Regarding your observations.... I will try to fix the issues regarding the caps lock highlight height.

I can't do much about the caret symbol. I could add an extra option to change the caret symbols? I will also filter out Emoji fonts.

When the width of the font is unusual and the text gets clipped, you must decrease the text width factor [OSD appearances].

Regarding mouse halo, I do not intend to add options per application / windows.

Best regards, Marius.

[EDITED] to add details.
Last edited by robodesign on 18 Jan 2018, 10:31, edited 2 times in total.
-------------------------
KeyPress OSD v4: GitHub or forum. (presentation video)
Quick Picto Viewer: GitHub or forum.
AHK GDI+ expanded / compilation library (on GitHub)
My home page.
robodesign
Posts: 934
Joined: 30 Sep 2017, 03:59
Location: Romania
Contact:

Re: [Script] KeypressOSD - Display key press on screen

18 Jan 2018, 10:23

Hello, guys!

Today's new version is ready, with really welcomed improvements.

Download AHK file from or use the Update now option:
https://github.com/marius-sucan/KeyPress-OSD
http://marius.sucan.ro/media/files/blog ... ss-osd.ahk
Please use AHK_H v1.1.27 [unicode] to execute this script.

v3.99.5 (2018-01-18)
- [major bug fix] it now sets the same keyboard layout to the AHK script window as the host app when initiating Alternative Typing Mode [with Ctrl + Insert]; MANY THANKS to drugwash!!! u da best! ;-)
- [bug fix] now it detects keyboard layout changes even when a Metro/Win 10 app is active; again, thanks to drugwash
- reduced file size [by another 6 kilobytes] and simplified keyboard detection system

@drugwash, @burque505: Please test if keyboard layouts are properly detected.
@drugwash: Should I add an option for changing the caret symbols? I prepared it for this.

Best regards, Marius.
-------------------------
KeyPress OSD v4: GitHub or forum. (presentation video)
Quick Picto Viewer: GitHub or forum.
AHK GDI+ expanded / compilation library (on GitHub)
My home page.
User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: [Script] KeypressOSD - Display key press on screen

18 Jan 2018, 11:18

No need for thanks, that code is in the AHK sources, I just digged it out. :)

Caret symbol should not be changed on a per-font basis, and manually above all - it should be fixed. I'll think of a workaround - if only I could understand how text is manipulated, so far I only messed with the GUI and some options.
You know my opinion on filtering fonts; what if the user wants precisely a large view of emojis or other symbols in some application? I made filtering optional. Also added an option to select font quality.
OSD width calculation routine must be fixed to yield correct size for all kinds of fonts, as unusual as they may be. Will see what can be done about that.
I'll probably try to add an exclusion list too, those halos are annoying in fullscreen applications.

I had a mod ready but will look over this new version first, see what can be merged and fixed.
In that mod I have fixed data types and command/function names to start with uppercase as described in the documentation (although AHK doesn't care), just for better code readability, to easily distinguish between built-in functions/commands/data types and user-defined variables, functions or subroutines.

Layout are correctly detected with your new version here on XP.
Part of my AHK work can be found here.
robodesign
Posts: 934
Joined: 30 Sep 2017, 03:59
Location: Romania
Contact:

Re: [Script] KeypressOSD - Display key press on screen

18 Jan 2018, 12:12

Okay, great, thanks. Looking forward to your mod and workarounds/improvements.

OSD width and height, indeed should be better calculated.

Some days ago, I also attempted to add dynamic shrinking of text size [based on min/max sizes] and I failed.

Thank you very much.

[EDIT]: Please update file. Fixed a small bug regarding daddy keys, erm... dead keys.

Best regards, Marius.
-------------------------
KeyPress OSD v4: GitHub or forum. (presentation video)
Quick Picto Viewer: GitHub or forum.
AHK GDI+ expanded / compilation library (on GitHub)
My home page.
User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: [Script] KeypressOSD - Display key press on screen

18 Jan 2018, 12:56

Haha, daddy keys. :lol:

Got a small fix here too in setColors(), percents must be removed from ctrl in if %ctrl% not in MouseHaloColor, otherwise the preview would still be displayed when changing mouse halo color, which is unwanted/unnecessary.

In what scenario would dynamic shrinking be beneficial?
Part of my AHK work can be found here.
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: [Script] KeypressOSD - Display key press on screen

18 Jan 2018, 14:02

Hi, just did a test, only found one thing, almost not worth mentioning. In German, when typing é, pressing (English =) is not reported as a dead key, but when typing û (English `), [Dead key] appears in the display.
When typing è, pressing (English +, i.e. Shift+=) also causes [Dead key] to appear in the display.
Win 7, 64-bit, AHK_H 1.1.27.06 U64 (U32 throws ComObjCreate errors, class not registered).
Regards,
burque505
robodesign
Posts: 934
Joined: 30 Sep 2017, 03:59
Location: Romania
Contact:

Re: [Script] KeypressOSD - Display key press on screen

18 Jan 2018, 15:53

Hello, guys!

Thank you very much for the report, burque.

@burque505: here, U32 does not crash. Please tell me more precisely what crashes, what lines/section. Please also go to Preferences > Key history and press the dead key repeatedly and tell me the VK/SC of that key that is not recognized accordingly. Also, which German keyboard is it? IBM, Swiss, standard?

@drugwash: I can type quite long lines and would like to see its entirety... I usually set a big font: Arial, 26 and it could scale down to 20, for me. But this feature, should not cripple it.... Or render it noticeably slower than it is...

Best regards, Marius.
-------------------------
KeyPress OSD v4: GitHub or forum. (presentation video)
Quick Picto Viewer: GitHub or forum.
AHK GDI+ expanded / compilation library (on GitHub)
My home page.
User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: [Script] KeypressOSD - Display key press on screen

19 Jan 2018, 06:48

Font autosize can be done using either Fnt_FontSizeToFitDT() or Fnt_FontSizeToFit() from the Fnt library. However it doesn't always calculate right and not for all fonts. I've done some limited testing using the provided example in the package.

On the other hand, GUI sizing can be done quite well using Fnt_GetStringSizeDT() - which in my tests performed better for certain fonts - or Fnt_GetStringSize() from the same package, adding some overhead for width (control margins, if Edit) and height (GUI_CTL_VERTICAL_DEADSPACE, dunno where this comes from).
Part of my AHK work can be found here.
robodesign
Posts: 934
Joined: 30 Sep 2017, 03:59
Location: Romania
Contact:

Re: [Script] KeypressOSD - Display key press on screen

19 Jan 2018, 13:26

Hello!

Today's new version features bug fixes mostly:

v3.99.6 (2018-01-19)
- fixed regression: never display OSD was not working
- fixed a bug that caused crashes when typing
- some improvements to Align OSD to right feature
- and various other glitches

Best regards, Marius
-------------------------
KeyPress OSD v4: GitHub or forum. (presentation video)
Quick Picto Viewer: GitHub or forum.
AHK GDI+ expanded / compilation library (on GitHub)
My home page.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: a_bolog and 69 guests