屏幕键盘 —— OSK() v2.2

许多实用脚本和封装函数, 可以让您编写脚本更加便捷高效

Moderators: tmplinshi, arcticir

feiyue
Posts: 349
Joined: 08 Aug 2014, 04:08

屏幕键盘 —— OSK() v2.2

26 Oct 2018, 00:30

写了个简单的屏幕键盘,可以加入到开机启动脚本中,希望大家喜欢!

Code: Select all

/*
;------------------------------------
;  屏幕键盘 —— OSK() v2.2  By FeiYue
;
;  这是一个类似 Windows 屏幕键盘的小工具
;  写成函数形式,便于在开机脚本中使用
;------------------------------------
*/

OSK()

OSK(f="")
{
  static title, myid, arr, NewName, ShowOSK, LButtonDown, MouseIsOver
  ListLines, % (lls:=A_ListLines)?"Off":"Off"
  if (!title)
  {
    ; title:="屏幕键盘"
    title:="On-Screen Keyboard"
    NewName:={ "":"Space", Caps:"CapsLock"
      , App:"AppsKey", PrScn:"PrintScreen", ScrLk:"ScrollLock"
      , "↑":"Up", "↓":"Down", "←":"Left", "→":"Right" }
    ShowOSK:=Func(A_ThisFunc).Bind("")
    LButtonDown:=Func(A_ThisFunc).Bind("LButtonDown")
    MouseIsOver:=Func(A_ThisFunc).Bind("MouseIsOver")
  }
  if (f="MouseIsOver")
  {
    MouseGetPos,,, id
    return (id=myid and myid)
  }
  ListLines, %lls%
  if (f="HotkeyOn" or f="HotkeyOff")
  {
    f:=(f="HotkeyOn") ? "On":"Off"
    Hotkey, If, % MouseIsOver
    For k,v in StrSplit(",^,!,+,#,*", ",")
      Hotkey, $%v%LButton, %LButtonDown%, %f%
    Hotkey, If
    return
  }
  else if (f="LButtonDown")
  {
    Critical
    MouseGetPos,,, id, cid, 2
    if !(id=myid and myid)
      return
    if (cid="")
    {
      WinGetPos, wx, wy, ww,, ahk_id %myid%
      CoordMode, Mouse
      MouseGetPos, x1, y1
      ;------------------
      ListLines, % (lls:=A_ListLines)?"Off":"Off"
      timeout:=A_TickCount+3000
      State:=GetKeyState("LButton","P") "|" GetKeyState("LButton")
      While (A_TickCount<timeout)
      and (State=GetKeyState("LButton","P") "|" GetKeyState("LButton"))
        Sleep, 20
      ListLines, %lls%
      ;------------------
      if (x1>wx+ww-60 and x1<wx+ww and y1>wy and y1<wy+30)
      {
        %A_ThisFunc%("Close")
        return
      }
      MouseGetPos, x2, y2
      WinMove, ahk_id %myid%,, wx+x2-x1, wy+y2-y1
      return
    }
    ControlGetText, k,, ahk_id %cid%
    if k in Ctrl,Alt,Shift,Win
    {
      GuiControlGet, isEnabled, OSK: Enabled, % arr[k][1]
      For i,v in arr[k]
        GuiControl, OSK: Disable%isEnabled%, %v%
      if (isEnabled)
        return
      k:=(k="Win") ? "LWin" : k
    }
    s:=InStr(k," ") ? SubStr(k,0) : k
    s:=(v:=NewName[s]) ? v : s, s:="{" s "}"
    For i,k in StrSplit("Ctrl,Alt,Shift,Win", ",")
    {
      GuiControlGet, isEnabled, OSK: Enabled, % arr[k][1]
      if (!isEnabled)
      {
        For j,v in arr[k]
          GuiControl, OSK: Enable, %v%
        v:=(k="Win") ? "LWin" : k
        s={%v% Down}%s%{%v% Up}
      }
    }
    SendLevel, 10
    SendInput {Blind}%s%
    return
  }
  else if (f="Close")
  {
    Gui, OSK: Destroy
    %A_ThisFunc%("HotkeyOff"), myid:=""
    return
  }
  else if (f!="")
    return
  if (myid)
  {
    %A_ThisFunc%("Close")
    return
  }

  ; 键盘布局:[ 键名,宽度,间隔 ]
  ; Keyboard layout: [keyname, width, spacing]
  w1:=40, w2:=55, w3:=w1*14+2*13, arr:=[]

  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] ]

  ; 0x08000000  Click not to activate
  Gui, OSK: +AlwaysOnTop +Owner +E0x08000000 +Hwndmyid
  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% r0.8 -Wrap Hwndid, % k:=v.1
      if k in Ctrl,Alt,Shift,Win
        (!arr[k] && arr[k]:=[]), arr[k].Push(id)
    }
  }
  Gui, OSK: Show, NA, %title%
  %A_ThisFunc%("HotkeyOn")
  Try Menu, Tray, Delete, OSK
  Menu, Tray, Add, OSK, %ShowOSK%
  Menu, Tray, Default, OSK
  Menu, Tray, Click, 1
  For i,k in StrSplit("Ctrl,Alt,Shift,Win", ",")
  {
    SendInput {L%k% Up}
    SendInput {R%k% Up}
  }
  return
}

;
Last edited by feiyue on 09 Apr 2022, 23:54, edited 8 times in total.
lx930129
Posts: 10
Joined: 13 Sep 2016, 03:41

Re: 屏幕键盘 v1.5

05 Nov 2018, 21:41

FeiYue大神的作品总是很有意思,屏幕键盘美观大方,还可以跟实体键盘一样触发热键。
User avatar
huyaowen
Posts: 109
Joined: 28 Jul 2014, 01:15

Re: 屏幕键盘 v1.5

28 Mar 2020, 03:37

好用,感谢。
feiyue
Posts: 349
Joined: 08 Aug 2014, 04:08

Re: 屏幕键盘 —— OSK() v2.1

27 Apr 2021, 07:37

更新到v2.1版,鼠标点击屏幕键盘时不会转移焦点,而且支持远程控制的鼠标操作。 :dance: :beer:
linpinger
Posts: 20
Joined: 29 Dec 2013, 22:51
Location: 神州
Contact:

Re: 屏幕键盘 —— OSK() v2.1

23 Aug 2021, 01:53

最近正被osk困扰,这个真棒
feiyue
Posts: 349
Joined: 08 Aug 2014, 04:08

Re: 屏幕键盘 —— OSK() v2.2

09 Apr 2022, 23:58

更新到v2.2版,初次运行显示屏幕键盘,再次运行可以取消屏幕键盘。

Return to “脚本函数”

Who is online

Users browsing this forum: No registered users and 12 guests