Virtual Keyboard

Just starting out? Need help? Post your questions and find answers here.
Lima
User
User
Posts: 40
Joined: Tue Jul 14, 2015 2:52 pm

Virtual Keyboard

Post by Lima »

It is possible to have a virtual keyboard in PB such as
'Free Virtual Keyboard on
https://freevirtualkeyboard.com, among others?
My main question is how to send the pressed key to the active field of any application.

My thanks in advance to anyone who devotes some time to this matter.
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Virtual Keyboard

Post by Sicro »

Have a look at my post signature:
PB-CodeArchiv-Rebirth/Keyboard_Mouse/SimulateKeyboardMouseInputs.pbi
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Lima
User
User
Posts: 40
Joined: Tue Jul 14, 2015 2:52 pm

Re: Virtual Keyboard

Post by Lima »

Thank you very much Sicro.
But I think that I can't solve the problem with that software.
The clicked key must go to the application that has the focus at the moment of the click.

Thanks.
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Virtual Keyboard

Post by Sicro »

Lima wrote: Sun May 09, 2021 8:58 pm But I think that I can't solve the problem with that software.
The clicked key must go to the application that has the focus at the moment of the click.
The trick now is to create a virtual keyboard window that doesn't steal focus from other windows.

Here is a code that creates a virtual keyboard with the letter keys "a" and "b":

Code: Select all

IncludeFile "Path to PB-CodeArchiv-Rebirth/Keyboard_Mouse/SimulateKeyboardMouseInputs.pbi"

Enumeration Window
  #Window_Main
EndEnumeration

Enumeration Gadget
  #Button_a
  #Button_b
EndEnumeration

If Not OpenWindow(#Window_Main, 0, 0, 160, 100, "Virtual Keyboard")
  Debug "Error: OpenWindow"
  End
EndIf

; Prevent the window from being activated (getting the focus)
exStyle = GetWindowLong_(WindowID(#Window_Main), #GWL_EXSTYLE)
SetWindowLong_(WindowID(#Window_Main), #GWL_EXSTYLE, exStyle | #WS_EX_NOACTIVATE)

; Set window always on top of all other open windows
StickyWindow(#Window_Main, #True)

ButtonGadget(#Button_a, 15, 20, 50, 50, "a")
ButtonGadget(#Button_b, 95, 20, 50, 50, "b")

Repeat
  event = WaitWindowEvent()
  If event = #PB_Event_Gadget
    Select EventGadget()
      Case #Button_a
        Simulate::ComputerKey('A', #True)
        Simulate::ComputerKey('A', #False)
      Case #Button_b
        Simulate::ComputerKey('B', #True)
        Simulate::ComputerKey('B', #False)
    EndSelect
  EndIf
Until event = #PB_Event_CloseWindow
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Lima
User
User
Posts: 40
Joined: Tue Jul 14, 2015 2:52 pm

Re: Virtual Keyboard

Post by Lima »

You're right Sicro.
The solution was in your module.
This was the big problem "The trick now is to create a virtual keyboard window that doesn't steal focus from other windows"

My big thanks to you.
Lima
User
User
Posts: 40
Joined: Tue Jul 14, 2015 2:52 pm

Re: Virtual Keyboard

Post by Lima »

(PureBasic 5.73,Win 10, Portuguese keyboard)

With the code below I get Ã.
But the Chr(191)="¿" and Chr(126)="~".
Is it possible to match the character that the key displays and the Sendinput function?

Thank you

Code: Select all

OpenWindow(0, 0, 0, 300,200, "Virtual Keyboard")
ButtonGadget(10, 15, 55, 50, 30,"Test")


SetWindowLong_(WindowID(0), #GWL_EXSTYLE,  GetWindowLong_(WindowID(0), #GWL_EXSTYLE) | #WS_EX_NOACTIVATE)

; Set window always on top of all other open windows
StickyWindow(0, #True)

 

Debug "Tilde= "+Asc("~");  =126
Debug "Chr(191)= "+Chr(191);  = ¿


Repeat
  event = WaitWindowEvent()
  
  Select event 
      Case #PB_Event_Gadget
   Select EventGadget()
      Case 10
 
     
      TipTap.INPUT
        TipTap\Type = #INPUT_KEYBOARD
        
            TipTap\ki\wVk =191
            TipTap\ki\dwFlags = 0
         SendInput_(1, @TipTap, SizeOf(INPUT))
         
;          TipTap\ki\wVk = 191
            TipTap\ki\dwFlags = #KEYEVENTF_KEYUP
            SendInput_(1, @TipTap, SizeOf(INPUT))
            
            
             TipTap\ki\wVk = #VK_LSHIFT
            TipTap\ki\dwFlags = 0
            SendInput_(1, @TipTap, SizeOf(INPUT))
            
            TipTap\ki\wVk ='A'
            TipTap\ki\dwFlags = 0
         SendInput_(1, @TipTap, SizeOf(INPUT))
         
;          TipTap\ki\wVk = 'A'
            TipTap\ki\dwFlags = #KEYEVENTF_KEYUP
            SendInput_(1, @TipTap, SizeOf(INPUT))
            
              
            TipTap\ki\wVk = #VK_LSHIFT
            TipTap\ki\dwFlags =  #KEYEVENTF_KEYUP
            SendInput_(1, @TipTap, SizeOf(INPUT))
            
         
        EndSelect
    EndSelect
    

  Until event = #PB_Event_CloseWindow

User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Virtual Keyboard

Post by Sicro »

Try this code:

Code: Select all

IncludeFile "Path to PB-CodeArchiv-Rebirth/Keyboard_Mouse/SendKeys[Win].pbi"

OpenWindow(0, 0, 0, 300,200, "Virtual Keyboard")
ButtonGadget(10, 15, 55, 50, 30,"Test")

SetWindowLong_(WindowID(0), #GWL_EXSTYLE,  GetWindowLong_(WindowID(0), #GWL_EXSTYLE) | #WS_EX_NOACTIVATE)

; Set window always on top of all other open windows
StickyWindow(0, #True)

Debug "Tilde= "+Asc("~");  =126
Debug "Chr(191)= "+Chr(191);  = ¿

Repeat
  event = WaitWindowEvent()
  
  Select event
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 10
          SendKeys(Chr(191) + "A")
      EndSelect
  EndSelect

Until event = #PB_Event_CloseWindow
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Lima
User
User
Posts: 40
Joined: Tue Jul 14, 2015 2:52 pm

Re: Virtual Keyboard

Post by Lima »

Hello Sicro.
It works. Now I can send the ascii code of the key that the result matches.

Thank you for the help.
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Virtual Keyboard

Post by Sicro »

Glad to hear it. Then I know why it didn't work with the other module and can fix it.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Post Reply