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

Post your working scripts, libraries and tools for AHK v1.1 and older
feiyue
Posts: 349
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()

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]
     ,["F6"],["F7"],["F8"],["F9",,15],["F10"],["F11"],["F12"]
     ,["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]
     ,["Alt",w2],["Win",w2],["App",w2],["Ctrl",w2]
     ,["←",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)
      j=
    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
  return

  OSKGuiClose:
  Gui, OSK: Destroy
  return

  RunOSK:
  k:=A_GuiControl
  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%}
    return
  }
  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%
  return
}

;
Last edited by feiyue on 27 Oct 2018, 12:20, edited 1 time in total.
User avatar
Nextron
Posts: 1391
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: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

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

26 Oct 2018, 16:21

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

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

burque505
Posts: 1734
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):
Regards,
burque505
Attachments
OSK-1.5.png
(37.51 KiB) Downloaded 62 times
User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

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!
Part of my AHK work can be found here.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: shawn_xwang, terraformars and 161 guests