On-Screen Keyboard —— OSK() v1.5

Post your working scripts, libraries and tools
Posts: 236
Joined: 08 Aug 2014, 04:08

On-Screen Keyboard —— OSK() v1.5

26 Oct 2018, 04:29

This is a small tool similar to the WinXP's On-Screen Keyboard.
I hope you like it. :dance:

Code: Select all

On-Screen Keyboard -- OSK() v1.5  By FeiYue

This is a small tool similar to the WinXP's On-Screen Keyboard.

Written in function form, easy to invoke in other scripts.



OSK() {

  static NewName:={ "":"Space", Caps:"CapsLock"
     , App:"AppsKey", PrScn:"PrintScreen", ScrLk:"ScrollLock"
     , "↑":"Up", "↓":"Down", "←":"Left", "→":"Right" }
  w1:=45, h1:=30, w2:=60, w3:=w1*14+2*13

  ; Keyboard layout: [key name, width, spacing]

  s1:=[ ["Esc"],["F1",,w3-w1*13-15*2-2*9],["F2"],["F3"],["F4"],["F5",,15]
     ,["PrScn",w2,10],["ScrLk",w2],["Pause",w2] ]

  s2:=[ ["~ ``"],["! 1"],["@ 2"],["# 3"],["$ 4"],["% 5"],["^ 6"]
     ,["&& 7"],["* 8"],["( 9"],[") 0"],["_ -"],["+ ="],["BS"]
     ,["Ins",w2,10],["Home",w2],["PgUp",w2] ]

  s3:=[ ["Tab"],["q"],["w"],["e"],["r"],["t"],["y"]
     ,["u"],["i"],["o"],["p"],["{ ["],["} ]"],["| \"]
     ,["Del",w2,10],["End",w2],["PgDn",w2] ]

  s4:=[ ["Caps",w2],["a"],["s"],["d"],["f"],["g"],["h"]
     ,["j"],["k"],["l"],[": `;"],[""" '"],["Enter",w3-w1*11-w2-2*12] ]

  s5:=[ ["Shift",w1*2],["z"],["x"],["c"],["v"],["b"]
     ,["n"],["m"],["< ,"],["> ."],["? /"],["Shift",w3-w1*12-2*11]
     ,["↑",w2,10+w2+2] ]

  s6:=[ ["Ctrl",w2],["Win",w2],["Alt",w2],["",w3-w2*7-2*7]
     ,["←",w2,10],["↓",w2],["→",w2] ]

  Gui, OSK: Destroy
  ; +E0x08000000  Click not to activate
  Gui, OSK: +AlwaysOnTop +Owner +E0x08000000
  Gui, OSK: Font, s12, Verdana
  Gui, OSK: Margin, 10, 10
  Gui, OSK: Color, DDEEFF
  Loop, 6 {
    if (A_Index<=2)
    For i,v in s%A_Index%
      w:=v.2 ? v.2 : w1, d:=v.3 ? v.3 : 2
      j:=j="" ? "xm" : i=1 ? "xm y+2" : "x+" d
      Gui, OSK: Add, Button, %j% w%w% h%h1% -Wrap gRunOSK, % v.1
  Gui, OSK: Show, NA, On-Screen Keyboard

  Gui, OSK: Destroy

  if k in Shift,Ctrl,Win,Alt
    v:=k="Win" ? "LWin" : k
    GuiControlGet, isEnabled, OSK: Enabled, %k%
    GuiControl, OSK: Disable%isEnabled%, %k%
    if (!isEnabled)
      SendInput, {Blind}{%v%}
  s:=InStr(k," ") ? SubStr(k,0) : k
  s:=(v:=NewName[s]) ? v : s, s:="{" s "}"
  For i,k in StrSplit("Shift,Ctrl,Win,Alt", ",")
    GuiControlGet, isEnabled, OSK: Enabled, %k%
    if (!isEnabled)
      GuiControl, OSK: Enable, %k%
      v:=k="Win" ? "LWin" : k
      s={%v% Down}%s%{%v% Up}
  SendInput, {Blind}%s%

Last edited by feiyue on 27 Oct 2018, 12:20, edited 1 time in total.
User avatar
Posts: 1377
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: On-Screen Keyboard —— OSK() v1.5

26 Oct 2018, 08:11

Screenshot would be nice? :P
User avatar
Delta Pythagorean
Posts: 567
Joined: 13 Feb 2017, 13:44
GitHub: DelPyth
Location: Somewhere in the US

Re: On-Screen Keyboard —— OSK() v1.5

26 Oct 2018, 16:21

(28.67 KiB) Not downloaded yet
I'm not the author, but this is a screenshot of the function/script.

- [AHK].......: Unicode 64-bit
- [OS].........: Windows 10.0.18362
- [GITHUB]...: github.com/DeltaPyth
- [PAYPAL]....: paypal.me/DelPyth
- [DISCORD]..: Delta#3324

Remember to use [code]CODE[/code] for your multi-line scripts.
Stay safe, stay inside, and remember to wash your hands for 20 seconds!
Posts: 1372
Joined: 22 Jan 2017, 19:37

Re: On-Screen Keyboard —— OSK() v1.5

27 Oct 2018, 08:25

Thanks, feiyue, works great. Here's a PNG (Note the arrows - the screenshot of the post above by Delta Pythagorean might have been from an ANSI copy of the script :), I'm pretty sure it needs UTF-8 with BOM):
(37.51 KiB) Downloaded 62 times
User avatar
Posts: 753
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania

Re: On-Screen Keyboard —— OSK() v1.5

28 Oct 2018, 02:53

@ feiyue: All Windows versions starting with ME (Millenium Edition) have a built-in OSK. XP's is uglier than yours - since you mentioned the similarity - but like all others it does what yours doesn't:
1. Display current layout
2. React to layout changes

Yes, there are many other keyboard layouts out there besides EN. Many people around the world use a single layout but it's not necessarily EN so for them your script would be of little to no use. Many other people have multiple layouts installed and such script should be able to react to layout changes, display the new layout promptly and send the correct keystrokes according to current layout. Your script is stuck on EN only.

Besides that, for some reason your script is sluggish, dragging the window doesn't move it until the mouse button is released (observed in XP).
Also, disabling buttons as opposed to setting them in a pressed state (and allowing toggle) is not a good idea, especially for Alt where in certain layouts right Alt (also marked as AltGr) performs a different function (Ctrl+Alt) than left Alt, and in my opinion is kinda counterintuitive generally to "lock" left button and "unlock" it by clicking its right counterpart.

Otherwise the GUI looks nice and the script does what it can do given the fixed layout so it's a good start. But you do have to get documented on layout changing and all the stuff related to it (see Keypress OSD, Klavier hero and other similar/related scripts around this board) if you want to build a valuable script. Please take all of this as constructive criticism. Good luck!
I've deleted my CloudMe account because of GDPR - the now legal base for privacy invasion and data theft.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: RicKami, Xtra and 30 guests