[Script] KeypressOSD - Display key press on screen

Post your working scripts, libraries and tools for AHK v1.1 and older
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

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

19 Jan 2018, 14:31

Hi Marius,
Testing new version. No failures now with U32 (maybe a glitch on my machine before? Don't know. It's gone, though).
Now, ANY dead key typed at the BEGINNING of a line will result in [Dead key] in the OSD. But not in any subsequent keypresses on the same line.
All languages.
Regards,
burque505
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, 14:38

Haha, I was about to report 'Never display OSD' not working, just stumbled into it minutes ago when trying to get the OSD out of my face. :)

I'm close to finding an alternative to the ugly caret but wasted hours trying to find out why a bloody message (EM_POSFROMCHAR) doesn't work at all, to no avail. :(
[EDIT]Is that message really Vista+ ? That would explain it. In that case, what would be the best alternative to it, except GetCaretPos() which doesn't work when Edit control is not focused?
Darn, I had the wrong constant because somehow certain messages are defined twice and they are very different in RichEdit vs Edit (see here)!

Is there anything you could do with this piece of code?

Code: Select all

 #SingleInstance force
 #NoEnv
 #MaxHotkeysPerInterval 500
 #MaxThreads 255
 #MaxThreadsPerHotkey 255
 #MaxThreadsBuffer On
 #WinActivateForce
 SetTitleMatchMode, 2
 SetBatchLines, -1
 ;SetWinDelay, 0
 ;SetControlDelay, 0
 ;SetKeyDelay, 0, 0
 ;SetMouseDelay, 0
 ListLines, Off
 SetWorkingDir, %A_ScriptDir%
DetectHiddenWindows, On
CoordMode, Tooltip, Screen

; valid caret styles: 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x11, 0x12

text := "This is text for testing purposes and should be long enough to get scrolled in the edit control"
Gui, +AlwaysOnTop -Border -Caption +ToolWindow +LastFound +HwndhGui
Gui, Margin, 2, 2
Gui, Color, 550055, 550055
Gui, Font, s24 bold cfeaafe, Arial
Gui, Add, Edit, xm ym w600 h40 -E0x200 ReadOnly -Multi -WantCtrlA -WantReturn -Wrap Right hwndhEdit vEdit, %text%
hFont := DllCall("SendMessage", "Ptr", hEdit, "UInt",  0x31,"UInt", 0, "UInt", 0)	; WM_GETFONT
Fnt_GetStringSizeDT(hFont, text, fW, fH)
Gui, Add, Text, x0 y0 w2 h%fH% 0x6 BackgroundTrans vcaret hwndhCaret,
DllCall("SetParent", "Ptr", hCaret, "Ptr", hEdit)
Gui, Show, y100 AutoSize NoActivate, Test caret
GuiControlGet, Edit, Pos
blinkT := DllCall("GetCaretBlinkTime")
SetTimer, blink, %blinkT%
return
;================================================================
blink:
DllCall("HideCaret", "Ptr", hEdit)
caretShow := !caretShow
GuiControl, % (caretShow ? "Show" : "Hide"), caret
return
;================================================================
~Left::
DllCall("SendMessage", "Ptr", hEdit, "UInt", 0xB0, "UIntP", s1, "Ptr", 0) ; EM_GETSEL
if (hGui != WinExist("A"))
	{
	if !s1
		return
	s1--
	}
DllCall("SendMessage", "Ptr", hEdit, "UInt", 0xB1, "UInt", s1, "UInt", s1) ; EM_SETSEL
sleep, 0
x := DllCall("SendMessage", "Ptr", hEdit, "UInt", 0xD6, "UInt", s1, "UInt", 0) ; EM_POSFROMCHAR
tooltip, s1=%s1% x=%x%
if (x<0)
	return
GuiControl, Move, caret, x%x%
return
;================================================================
~Right::
DllCall("SendMessage", "Ptr", hEdit, "UInt", 0xB0, "UIntP", s1, "Ptr", 0) ; EM_GETSEL
if (hGui != WinExist("A"))
	{
	if (s1>= StrLen(text))
		return
	s1++
	}
DllCall("SendMessage", "Ptr", hEdit, "UInt", 0xB1, "UInt", s1, "UInt", s1) ; EM_SETSEL
sleep, 0
x := DllCall("SendMessage", "Ptr", hEdit, "UInt", 0xD6, "UInt", s1, "UInt", 0) ; EM_POSFROMCHAR
tooltip, s1=%s1% x=%x%
if (x> EditW-2) OR x<0
	x := EditW-2
GuiControl, Move, caret, x%x%
return

#include Fnt.ahk
Last edited by Drugwash on 19 Jan 2018, 17:08, edited 4 times in total.
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, 16:18

Hello, guys:

@burque: that is correct behavior, if only typing mode is off. Is it off or on?Please also let me know if you have alternative hooks enabled. Glad to hear you get no crashes anymore on u32.

@drugwash: ooowww, that's nice to hear. I hope to like the solution ;-). Really curious about your new version. I don't know exactly what you are talking about, hehe.

Edit. Please make sure there is a way to display the dead key symbol when pressed.

Edit. Two. Do what with the code? Please be more specific. Drugwash.

Please also note, the edit field gets all flickering with big fonts when navigating with the actual caret. At least, in my tests.

Best regards, Marius.
Last edited by robodesign on 20 Jan 2018, 05:03, edited 1 time 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.
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, 17:26

Sample code is posted above. It works by retrieving the hidden caret's position in the string and then moving the specially-styled text control at that position. A timer is blinking the fake caret while hiding the real one when the Edit is focused.
The code contains some extra checks to make sure position is within boundaries and doesn't jump twice when focused.
Dunno how you actually insert the dead key symbols in the text string, didn't look much at that side of the script but I hope it would work.
There are basically only three colors available for the fake caret, using styles 0x4, 0x5 or 0x6 which produce solid block character. Styles 0x7, 0x8 and 0x9 produce empty blocks of the same respective colors, visible when fake caret width is larger than 2, but those styles may only be useful for very large font sizes or fonts with large spacing between characters.

Anyway, that's just a proof of concept, no idea if it will fit the main script or not; as mentioned above I wasted a lot of time trying to make things work and now I'm too tired for other tests. If you can fit the relevant bits to the main script and test it would be fine.
There's no flickering here in the sample with Arial 24 but maybe text is too short or font too small. And remember it's XP, who used to be sluggish and flicker in older versions. Just test the sample, modify font size and text string as needed, see how it behaves. The original caret should not be visible even when Edit is focused.
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, 17:40

Merci beaucoup. I will test it out tomorrow. Really curious about it. Thank you very much.

Good night :-)
-------------------------
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

20 Jan 2018, 04:17

Hello, drugwash!

I just tested the prototype you posted. It flickers only when the text gets scrolled, but not for every movement. Now, I wonder how and if it can be adapted to work with making selections, displaying dead key symbols and finally, manage unicode chars formed with an additional modifier char.

What do you think of these? can it be pulled off?

PS. Being just a prototype, no surprise for it being a buggy ;-)..., when I reach the end and press a few times more Right, it does not go Left when needed. And the caret has the tendency to be placed a little on top of the letters.

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

20 Jan 2018, 05:41

Oh well, that's the abomination for you. :) It doesn't flicker at all here, maybe just a tiny bit when reaching a far end of the control and has to bring a new batch of characters into view. The original caret may flicker in the background (when Edit is focused) since it's being hidden on a timer basis not when Left/Right are pressed.

Are you sure you got the very latest version of the sample code? I've modified it a few times last night since first posted.
As for the caret being placed on top of characters, that may be a matter of API miscalculation or a too large fake caret width. Original caret is 1px wide, I made this one 2px just to be more visible but width can be modified in script. Otherwise it always takes the position of the original caret so if that one is off, the fake one will be off too. And it may also depend on font type, native italic fonts or those with unusual escapement may be overdrawn, but same would happen with the original caret. If it's not that then maybe you'd post a screenshot, if possible, so I could understand better.

Basically the code just follows the original caret position, so in the main script you'd only have to update current caret position using EM_SETSEL instead of inserting the ugly caret character that doesn't show up properly in many fonts, and then update the fake caret position. All those extra checks in the sample code may not be necessary. There will be no change in how characters are inserted in the string to be displayed, no messing with dead chars, no other fancy stuff.

For now I'm busy searching for information on other keyboard layout related issues so I won't be abe to operate those changes in the main script, especially when I'm not familiar with the string manipulations and all that in the script. And anyway this is not time critical.
One thing to check would be how the main script behaves with this change in, considering XP would have to deal again with an Edit, since there's no original caret in a Static (Text) field.
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

20 Jan 2018, 06:13

Hello!

@drugwash: I can see this prototype as a future solution... but it needs work. However, I am unfamiliar with these things. I will wait for you patiently and see what you come up with.

I made some small changes to the script today:

v3.99.7 (2018-01-20)
- reduced further flickering when using Align to right for the OSD.
- now it always indicates when a modifier is pressed, even in only typing mode by changing the text caret to a gray block :-).

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

20 Jan 2018, 08:49

Yes, I figured it would need some work and I guess 'lola' will be dismissed. :)
It'll take a while before I actually get to that part so you may take advantage of this and fix/modify/add whatever needed in the mean time.
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

20 Jan 2018, 09:18

Yes, Lola and caretzescu.....:-)

Now, I only want to add keyboard shortcuts customization. And I have an idea how to make it better synchronize with the host app when jumping between words. That's all I have planned.

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

20 Jan 2018, 19:59

Maybe this kind of live, accurate detection would be more useful than the hardcoded languages file:
20180121025623.png
20180121025623.png (23.31 KiB) Viewed 3911 times
There are still details to take care of so I won't be posting any code yet.
No news o the fake caret, didn't work on it today… erm, I mean yesterday since it's 3AM already.

Oh and where did the caret halo code go…? :?
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

21 Jan 2018, 06:58

Hello!

@drugwash: You mean layout name detection without hardcoded names? That would be LOVELY ;-). I would be happy if dead keys detection could be automatic as well, without hard-coded VKs. With both, then we could eliminate the lang file entirely. Dream like.

@drugwash: the caret halo stuff was moved into mouse functions file. I want the main OSD thread to be as light as possible.

I made a new version, I almost had it ready yesterday, but glitches kept me from releasing it.

v4.01 (2018-01-21)
- bug fixes and improvements for the new feature implemented yesterday
- improvements [hopefully] to the handling of Ctrl+A/Z/X/C/V... again

v4.00 (2018-01-20)
- [new] option to send keystrokes that reproduce text changes operated in the typed OSD content, when making caret jumps with Ctrl + Bksp / Del / Left / Right. This helps to ensure a consistent experience across apps and to remain in synch [not enabled by default, for now... I might change this]

The new feature is usable in most scenarios. I think I should have it on by default.

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.

@drugwash: On my plate, I only got now to add Customizable kbd shortcuts. But I will wait for your new edition, I am very curious. Make them hard-coded stuffz vanish into thin h-air! I will make new releases only if I get new bug reports or when I fix bugs I find myself.

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

21 Jan 2018, 10:24

Every OS version has its own list of supported languages and layouts, and all that information can be found in the system. It's just a matter of proper API (and other information) to reveal it. I still haven't figured out how to properly read and use some registry keys, so my test script mostly works but with possible exceptions.

There is a clear distinction between languages and layouts, and you need to understand it for a proper implementation of detection and display.
I didn't yet delve into the world of dead keys and composed characters, that will be a complicated matter. I need to first finish a few things I started before I do that. Actually I kinda have a thing with unfinished businesses.

You need to add a reliable mechanism for updating related files when any of them were changed on your side from one version to another, otherwise the script will run crippled or not at all. Currently it doesn't check for the mouse-functions version/size/hash or the caret halo routine presence hence the option won't work, it silently fails. And remember people may modify parts of the additional scripts (and/or the main script) and may wanna keep them for future upgrade/merge so make backups, don't just overwrite when updating. ;)

I hope 'customizable keyboard shortcuts' doesn't mean more of script's own hotkeys. I hate hotkeys! (I know, it's strange considering where we are now, but that's how I feel due to too many bad implementations in too many applications; actually this stupid browser just crashed while looking in the menu for all those damn hotkeys that cannot be disabled, so I had to take a partial screenshot and type this all over again)

Don't wait for me, I'll be working on various bits and pieces outside the main script - although more or less related to it - and it may take quite some time before deciding they could be merged in. There are also a few old quirks that need to be fixed in my mod, related to dragging, reading/saving settings, settings panels. I don't like frequent bugfix versions, people get confused and wary. So do your things, I'll do mine and we'll see where we get to. ;)
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

21 Jan 2018, 11:29

Hello!

Okay, thank you.

Yes, there's a clear distinction to be made between kbd layouts and languages.

When you run the update it ask if you want to abort ;-). I was about to lose a few days of work, because I clicked by mistake Update. Since that day I made it ask first. It downloads the zip file and un-packs it, over-writing everything. Apologies, but I do not plan to work on an advanced update system.

I am surprised you do not have the caret halo working. It should work. I suppose you checked the mouse-related functions file for CaretHalo(), right? It should be there.

Okay, I won't wait for you then, but... I am quite anxious about your edition. Today's post, with that screen shot just blew me away and the potential for that caret re-work.

I intend to do something very simple regarding shortcuts customization, you'll probably be disappointed ;-), how banale it will be. I won't add more shortcuts, actually I might turn off a few [by default].

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

21 Jan 2018, 12:08

When you run the update it ask if you want to abort […] It downloads the zip file and un-packs it, over-writing everything. Apologies, but I do not plan to work on an advanced update system.
I want to update, I just don't want to lose the possibly modified files. One may click on Yes by mistake, forgetting they have modified something. Yes, the unpacking routine also asks whether it should overwrite all or not, but that's another quirk that bugs me, because it shouldn't ask (according to the flags that M$ decided not to take into account) and I'd like to get rid of it. So that's another thing I'll have to do myself (create Backup folder, move files in there, then allow unpacking, at which point I have a backup and also no annoying overwrite confirmation). Ah, that's a very hard thing to do, dunno if I'll be able to do it! :P

Let me elaborate a bit. User downloads the updated main script manually from the links above. Saves it in the dedicated folder and then launches it. At that point, the script has no knowledge that the additional scripts had been modified by the user or updated on your site so it'll run as usual or at least it'll try to. Since there are no checks whatsoever in regard to additional scripts' versions or contents, the main script may run buggy or not at all.
So no, I have no CaretHalo() function in my mouse-functions file because the main script didn't tell me it needed a new, updated version of that file. And I didn't manually update because I'm not a Win10 user already accustomed to frequent updates (and assume I hadn't read all the forum posts to see if such change was mentioned or not; the changelog certainly stopped a few versions ago at 3.99.5 so no mention in there either). Capisci? ;)

Again, the language/layout detection you saw in the screenshot is a standalone script thrown together for testing purposes (same as the one for fake caret), it's not yet in a shape suitable for inclusion in the main script. I could publish it here if you really want but as mentioned before, some things are not quite right and other detections are necessary too (such as when the user adds or removes languages/layouts from the Control Panel).

As long as you don't add more shortcuts/hotkeys I'm happy. :)
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

21 Jan 2018, 14:42

Hello!

I searched on the internet how to detect KBD layout names and I came up with a solution of some sorts:

RegRead, langFriendlySysName, HKEY_LOCAL_MACHINE, SYSTEM\CurrentControlSet\Control\Keyboard Layouts\%kbLayoutRawCode%, Layout Text

I already implemented this, but of course I want to see your sample script. Maybe what I did is foolish ;-). I must test it on Win7 and WinXP.

The keypress-files.zip is of out-most importance to update each time, if you do it manually. Here it does not give me any additional prompts [to overwrite files]. And yes, I forgot to update the external change log file. Sorry. Updated now. And the zip. [attention: new language file]. I removed all the language names, except for those kbd layouts that are supported. When I made the list I distinguished some layout names.

v4.02 (2018-01-21)
- added support for (w/o Shift+) Home/End when the new option is activated [Send keystrokes to host app to reproduce OSD content]
- simplified keyboard detection mechanism even more; it reads from the registry the Keyboard Layout names, for unsupported ones; many thanks to Drugwash for the idea

PS. Edit. I think it would be better to use from there [the registry] the value from: Layout Display Name. The value is something like: @%SystemRoot%\system32\input.dll,-5210 . But, of course I have no clue how to transform that into the string that the DLL call returns.

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

21 Jan 2018, 15:57

You're getting close. ;) Here is the test script as it is currently, dunno how the day went by without me doing anything useful:

Code: Select all

#SingleInstance force
#NoEnv
#WinActivateForce
SetTitleMatchMode, 2
SetBatchLines, -1
SetWinDelay, -1
SetControlDelay, -1
SetKeyDelay, -1, -1
SetMouseDelay, -1
ListLines, Off
SetWorkingDir, %A_ScriptDir%
DetectHiddenWindows, On

Gui, Add, ListView, w500 h120, HKL|ISO|Input language|Keyboard layout
Gui, Add, StatusBar,,
SB_SetParts(60, 220)

key1 := "SYSTEM\CurrentControlSet\Control\Keyboard Layouts"
key2 := "Keyboard Layout\Substitutes"
hkl := GetHKL()
SetFormat, Integer, H
hkl+=0
SetFormat, Integer, D
GetKeyboardLayoutName(KLID)
cname:= GetLayoutDisplayName(KLID)
GetLocaleInfo(defLang, 0x2, DllCall("GetUserDefaultLCID"))
if r := DllCall("GetKeyboardLayoutList", "UInt", 0, "Ptr", 0)
	{
	VarSetCapacity(hklbuf, r*A_PtrSize, 0)
	count := DllCall("GetKeyboardLayoutList", "UInt", r, "Ptr", &hklbuf)
	}
/*
; This should be used in some manner but I'm not yet sure how
Loop
	{
	RegRead, pre, HKCU, Keyboard Layout\Preload, %A_Index%
	if !pre
		break
	RegRead, sub, HKCU, Keyboard Layout\Substitutes, %pre%
	}
*/
Loop, %count%
	{
	h := Hex2Str(NumGet(hklbuf, A_PtrSize*(A_Index-1), "UPtr"), 8, true)
	lcid := Hex2Str(h & 0xFFFF, 4)
	s := "0000" lcid	; not entirely correct, I've seen substitutions like d0000409
	LID := Hex2Str((h&0xFF0000)>>16, 4)
	if (h&0xF0000000)	; we got layout variant
		{
		RegRead, sub, HKCU, %key2%, %s%
		if !sub
			Loop, 16  ; arbitrary number, gotta be a better method to do this
				{
				sub := Hex2Str(A_Index-1, 4) lcid
				RegRead, v, HKLM, %key1%\%sub%, Layout Id
				if (v=LID)
					break
				}
		rkey := sub
		}
	else rkey := "0000" lcid
	Lname := GetLayoutDisplayName(rkey)
	GetLocaleInfo(name%h%:="", 0x2, h)
	GetLocaleInfo(s1%h%:="", 0x59, h), GetLocaleInfo(s2%h%:="", 0x5A, h)
	sname%h% := s1%h% "_" s2%h%
	LV_Add("", h, sname%h%, name%h%, Lname)
	}
LV_ModifyCol(1, "AutoSize")
LV_ModifyCol(2, "AutoSize Sort")
LV_ModifyCol(3, "AutoSize")
LV_ModifyCol(4, "AutoSize")
SB_SetText("Layouts: " count, 1)
SB_SetText("Current: " cname, 2)
SB_SetText("Default: " defLang, 3)
Gui, Show,, Installed languages and layouts
SetTimer, chkLang, 500
return

chkLang:
GetKeyboardLayoutName(KLID)
cname:= GetLayoutDisplayName(KLID)
SB_SetText("Current: " cname, 2)
return

GuiClose:
ExitApp
;================================================================
GetHKL(winT:="A")
;================================================================
{
r := DllCall("GetKeyboardLayout"
	, "UInt", DllCall("GetWindowThreadProcessId"
		, "Ptr", WinActive(winT)
		, "Ptr", 0)
	, "UPtr")
Return Hex2Str(r, 8, true)
}
;================================================================
GetKeyboardLayoutName(ByRef KLID)
;================================================================
{
VarSetCapacity(KLID, 18, 0)
DllCall("GetKeyboardLayoutName", "Str", KLID)
}
;================================================================
GetLocaleInfo(ByRef strg, loc, hkl:=0)
;================================================================
{
; LOCALE_SLANGUAGE=0x2, LOCALE_SISO639LANGNAME=0x59, LOCALE_SISO3166CTRYNAME=0x5A
Static A_CharSize := 2**(A_IsUnicode=true)
hkl := hkl ? hkl : GetHKL(), lcid := hkl & 0xFFFF
if sz := DllCall("GetLocaleInfo"
	, "UInt", lcid
	, "UInt", loc
	, "Ptr", 0
	, "UInt", 0)
	{
	VarSetCapacity(strg, sz*A_CharSize, 0)
	DllCall("GetLocaleInfo"
		, "UInt", lcid
		, "UInt", loc
		, "Str", strg
		, "UInt", sz)
	}
else strg := "Error " A_LastError " LCID: " lcid
}
;================================================================
SHLoadIndirectString(in)	; uses WStr for both in and out
;================================================================
{
VarSetCapacity(out, 2*(sz:=128), 0)
DllCall("shlwapi\SHLoadIndirectString", "Str", in, "Str", out, "UInt", sz, "Ptr", 0)
return out
}
;================================================================
GetLayoutDisplayName(subkey)
;================================================================
{
Static key := "SYSTEM\CurrentControlSet\Control\Keyboard Layouts"
RegRead, mui, HKLM, %key%\%subkey%, Layout Display Name
if !mui
	RegRead, Dname, HKLM, %key%\%subkey%, Layout Text
else
	Dname := SHLoadIndirectString(mui)
Return Dname
}
;================================================================
Hex2Str(val, len, x:=false)
;================================================================
{
SetFormat, IntegerFast, D
len := x ? len+2 : len
x := x ? "#" : ""
VarSetCapacity(out, len*2, 32)
DllCall("msvcrt\sprintf", "AStr", out, "AStr", "%" x "0" len "x", "Int64", val, "CDecl")
Return out
}
Layout Display Name would be the preferred string to get, if it exists. Next best is Layout Text. The former is not present for custom layouts, only the latter, so the script should take this into account (mine does).
One problem remains to be solved: the substitutions. I attempted something but it's more guesswork than reliable code. Run it on your systems, see how/if it works and if results are correct. Any change to the current language/layout should be promptly displayed in the statusbar, regardless of the focused application. But that's using a timer and I don't like it; been looking for an elegant solution yesterday but couldn't find one other than global hook using separate dll.
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

21 Jan 2018, 16:03

Okay, thank you very much!

I will look into it, tomorrow. However, the layout name is not very important to be displayied in real time, for now. Because... We have pressing issues with changing the hotkeys as required, in real time - to bind to new regional keys and change / update dead keys. These are the important aspects that would need to be fixed/found solutions for, if we want to eliminate restarting for every kbd layout change. Ideally, it should not rely on any hard coded dead key. If it would get to this point, it would be purr....fection...

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

21 Jan 2018, 20:08

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

22 Jan 2018, 11:02

Hello!

Today's new version brings bug fixes, considerably important ones:

v4.031 (2018-01-22)
- [new] option to mediate navigation keys for slow computers: left / right, backspace / delete, home / end
- further improvements for keyboard layout name detection, thanks to Drugwash
- improved the compatibility of the features that enable synchronization with the host app, for foreign layouts
- various bug fixes regarding enable/disable the displaying of single keys, never show OSD, and the typing modes; it was a mess
- as a consequence, show single keys shortcut toggles now never display the OSD;

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.

@drugwash: I added two functions from your sample script into mine. Now, it gets the MUI lang name, for unsupported layouts. I was already handling substitutions properly, see my script. You can test by removing the content of lang.ini [except for LoadedLangz var] and it will recognize all substitutions with proper names. Your sample, indeed, did not get the proper names for substitutions. Thanks for the link, but I am not good with DLL calls as you are, by far.


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: No registered users and 130 guests