Author Topic: $VIRTUALKEYBOARD Alternative for Windows and Linux  (Read 1979 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • GitHub
$VIRTUALKEYBOARD Alternative for Windows and Linux
« on: April 19, 2021, 05:15:17 pm »
There used to be a keyword for QB64 that would pull up the virtual keyboard for Android development. The keyword still exists in QB64 but is deprecated and not used. This is my attempt at making something similar for Windows and Linux. Enjoy. For Linux, you will need to install onboard from the command line in order to use the function.

Code: QB64: [Select]
  1.  
  2.  
  3. VirtualKeyboard 1
  4. Input "Test the keyboard! ", test
  5. Print test
  6.  
  7.  
  8. VirtualKeyboard 0
  9.  
  10. Sub VirtualKeyboard (toggle As _Byte)
  11.     $If WINDOWS Then
  12.         Type PROCESSENTRY32
  13.             As Long dwSize, cntUsage, th32ProcessID
  14.             $If 64BIT Then
  15.             As Long padding
  16.             $End If
  17.             As _Unsigned Long th32DefaultHeapID
  18.             $If 64BIT Then
  19.             As Long padding2
  20.             $End If
  21.             As Long th32ModuleID, cntThreads, th32ParentProcessID, pcPriClassBase, dwFlags
  22.             As String * 260 szExeFile
  23.         End Type
  24.         Type STARTUPINFO
  25.             As Long cb
  26.             $If 64BIT Then
  27.             As Long padding
  28.             $End If
  29.             As _Offset lpReserved, lpDesktop, lpTitle
  30.             As Long dwX, dwY, dwXSize, dwYSize, dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags
  31.             As Integer wShowWindow, cbReserved2
  32.             $If 64BIT Then
  33.             As Long padding2
  34.             $End If
  35.             As _Offset lpReserved2, hStdInput, hStdOutput, hStdError
  36.         End Type
  37.         Type PROCESS_INFORMATION
  38.             As _Offset hProcess, hThread
  39.             As Long dwProcessId
  40.             $If 64BIT Then
  41.             As Long padding
  42.             $End If
  43.         End Type
  44.         Declare Dynamic Library "Kernel32"
  45.             Function CreateToolhelp32Snapshot& (ByVal dwFlags As Long, Byval th32ProcessID As Long)
  46.             Function Process32First%% (ByVal hSnapshot As _Offset, Byval lppe As _Offset)
  47.             Function Process32Next%% (ByVal hSnapshot As _Offset, Byval lppe As _Offset)
  48.             Function OpenProcess& (ByVal dwDesiredAccess As Long, Byval bInheritHandle As _Byte, Byval dwProcessId As Long)
  49.             Function TerminateProcess%% (ByVal hProcess As _Offset, Byval uExitCode As _Unsigned Long)
  50.             Function CloseHandle%% (ByVal hObject As _Offset)
  51.         End Declare
  52.             Function CreateProcess%% Alias "CreateProcessA" (ByVal lpApplicationName As _Offset, Byval lpCommandLine As _Offset, Byval lpProcessAttributes As _Offset, Byval lpThreadAttributes As _Offset, Byval bInheritHandles As Integer, Byval dwCreationFlags As Long, Byval lpEnvironment As _Offset, Byval lpCurrentDirectory As _Offset, Byval lpStartupInfor As _Offset, Byval lpProcessInformation As _Offset)
  53.             $If 32BIT Then
  54.                 Function Wow64DisableWow64FsRedirection%% (ByVal oldvalue As _Offset)
  55.             $End If
  56.         End Declare
  57.         Const TH32CS_SNAPPROCESS = &H00000002
  58.         Const PROCESS_TERMINATE = &H0001
  59.         Const CREATE_NO_WINDOW = &H8000000
  60.         Dim As _Byte ok
  61.         Select Case toggle
  62.             Case 1
  63.                 $If 32BIT Then
  64.                     Dim As _Offset oldvalue
  65.                     Dim As _Byte result
  66.                     result = Wow64DisableWow64FsRedirection(oldvalue)
  67.                 $End If
  68.                 Dim As STARTUPINFO si
  69.                 si.cb = Len(si)
  70.                 si.hStdInput = 0
  71.                 Dim As PROCESS_INFORMATION pi
  72.                 Dim As _Offset lpApplicationName
  73.                 Dim As String fullcmd: fullcmd = "cmd /c osk" + Chr$(0)
  74.                 Dim As String lpCommandLine: lpCommandLine = fullcmd
  75.                 Dim As _Offset lpProcessAttributes, lpThreadAttributes
  76.                 Dim As Integer bInheritHandles: bInheritHandles = 1
  77.                 Dim As Long dwCreationFlags: dwCreationFlags = CREATE_NO_WINDOW
  78.                 Dim As _Offset lpEnvironment, lpCurrentDirectory
  79.                 ok = CreateProcess(lpApplicationName, _Offset(lpCommandLine), lpProcessAttributes, lpThreadAttributes, bInheritHandles, dwCreationFlags, lpEnvironment, lpCurrentDirectory, _Offset(si), _Offset(pi))
  80.             Case 0
  81.                 Dim As Long snap
  82.                 snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
  83.                 Dim As PROCESSENTRY32 pEntry
  84.                 pEntry.dwSize = Len(pEntry)
  85.                 Dim As _Byte hRes
  86.                 hRes = Process32First(snap, _Offset(pEntry))
  87.                 While hRes
  88.                     If _StrCmp(Left$(pEntry.szExeFile, InStr(pEntry.szExeFile, ".exe" + Chr$(0)) + 3), "osk.exe") = 0 Then
  89.                         Dim As Long hProcess
  90.                         hProcess = OpenProcess(PROCESS_TERMINATE, 0, pEntry.th32ProcessID)
  91.                         If hProcess <> 0 Then
  92.                             Dim As _Byte terminate, closeh
  93.                             terminate = TerminateProcess(hProcess, 9)
  94.                             closeh = CloseHandle(hProcess)
  95.                         End If
  96.                     End If
  97.                     hRes = Process32Next(snap, _Offset(pEntry))
  98.                 Wend
  99.                 closeh = CloseHandle(snap)
  100.         End Select
  101.     $ElseIf LINUX Then
  102.         Select Case toggle
  103.         Case 1
  104.         Shell _Hide _DontWait "dbus-send --type=method_call --dest=org.onboard.Onboard /org/onboard/Onboard/Keyboard org.onboard.Onboard.Keyboard.Show"
  105.         Case 0
  106.         Shell _Hide _DontWait "dbus-send --type=method_call --dest=org.onboard.Onboard /org/onboard/Onboard/Keyboard org.onboard.Onboard.Keyboard.Hide"
  107.         End Select
  108.     $End If
« Last Edit: May 06, 2021, 08:40:58 am by SpriggsySpriggs »
Shuwatch!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • GitHub
Re: $VIRTUALKEYBOARD Alternative for Windows and Linux
« Reply #1 on: April 30, 2021, 05:16:48 pm »
I've updated the original post to also work with Linux. Requires an installation of onboard
Shuwatch!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • GitHub
Re: $VIRTUALKEYBOARD Alternative for Windows and Linux
« Reply #2 on: May 05, 2021, 02:50:21 pm »
On Discord, @Ashish pointed out that it wasn't working in 32 bit Windows. This was caused by an issue that is present in several programming languages and caused by Windows redirecting to 64 bit file directories. Thankfully, easily fixed by making one function call. Now it works in 32 and 64 bit Windows.
« Last Edit: May 05, 2021, 03:02:06 pm by SpriggsySpriggs »
Shuwatch!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: $VIRTUALKEYBOARD Alternative for Windows and Linux
« Reply #3 on: May 05, 2021, 04:06:35 pm »
I've got a virtual keyboard here as well, if you want to check it out sometime: https://www.qb64.org/forum/index.php?topic=3608.0
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • GitHub
Re: $VIRTUALKEYBOARD Alternative for Windows and Linux
« Reply #4 on: May 05, 2021, 04:15:47 pm »
Oh, yeah that thing. I remember seeing that. Mine just brings up the OS's on-screen keyboard.
Shuwatch!

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
Re: $VIRTUALKEYBOARD Alternative for Windows and Linux
« Reply #5 on: May 06, 2021, 01:54:47 am »
Thanks! Now it works for me
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials