Get all textual input (no matter locale) with KeyboardInkey()?

Just starting out? Need help? Post your questions and find answers here.
Quin
Enthusiast
Enthusiast
Posts: 279
Joined: Thu Mar 31, 2022 7:03 pm
Location: United States
Contact:

Get all textual input (no matter locale) with KeyboardInkey()?

Post by Quin »

Hi,
I'm writing a program where I need to capture all input by the user (i.e. letters and numbers), regardless of their keyboard language. StrinGadget won't work for this specific use case, sadly. So I turned to KeyboardInkey(). The following code sample works, accept backspace and return give me non-printable characters, so they're pretty hard to filter out. What's the best way to go about this? Hook just those two keys with an if block, like I have for my escape logic? Are there more keys KeyboardInkey() catches that I don't want?
My code is as follows:

Code: Select all

InitSprite()
InitKeyboard()

OpenWindow(0, 480, 640, 400, 400, "Tester")
OpenWindowedScreen(WindowID(0), #PB_Ignore, #PB_Ignore, #PB_Ignore, #PB_Ignore)
KeyboardMode(#PB_Keyboard_AllowSystemKeys)
Repeat
  Event = WaitWindowEvent()
  If Event = #PB_Event_CloseWindow
    End
  EndIf
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Escape)
    End
  Else
    Result$ = KeyboardInkey()
    If Result$ <> ""
      MessageRequester("Info", Result$)
    EndIf
  EndIf
ForEver
Thanks in advance for any help!
PB v5.40/6.10, Windows 10 64-bit.
16-core AMD Ryzen 9 5950X, 128 GB DDR5.
User avatar
spikey
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Get all textual input (no matter locale) with KeyboardInkey()?

Post by spikey »

Quin wrote: Thu Dec 01, 2022 5:32 pm Are there more keys KeyboardInkey() catches that I don't want?
Probably, KeyboardInkey() will also send through some control keys too which I'm guessing you won't want either (eg, Ctrl-C, Ctrl-Z...). You can either filter out those keys you don't want or you can filter in those keys you do want, depends which list is shorter I guess.

Code: Select all

InitSprite()
InitKeyboard()

#Accept = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,.;:"

OpenWindow(0, 480, 640, 400, 400, "Tester")
OpenWindowedScreen(WindowID(0), #PB_Ignore, #PB_Ignore, #PB_Ignore, #PB_Ignore)
KeyboardMode(#PB_Keyboard_AllowSystemKeys)
Repeat
  Event = WaitWindowEvent()
  If Event = #PB_Event_CloseWindow
    End
  EndIf
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Escape)
    End
  Else
    Result$ = KeyboardInkey()
    If FindString(#Accept, Result$)
      MessageRequester("Info", Result$)
    EndIf
  EndIf
ForEver
It depends which locales you want to target. If you just want to cover the Latin characters and north-western European languages then you could feasibly filter on the ascii code of the character, see https://www.purebasic.com/documentation/string/asc.html. Printable characters are in the range 33 - 126 and 161 - 255, plus space which is 32, you probably don't want any others. If you want to cover non-Latin character set languages though it gets way more complicated. There is no "one size fits all" solution.

Code: Select all

InitSprite()
InitKeyboard()

OpenWindow(0, 480, 640, 400, 400, "Tester")
OpenWindowedScreen(WindowID(0), #PB_Ignore, #PB_Ignore, #PB_Ignore, #PB_Ignore)
KeyboardMode(#PB_Keyboard_AllowSystemKeys)
Repeat
  Event = WaitWindowEvent()
  If Event = #PB_Event_CloseWindow
    End
  EndIf
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Escape)
    End
  Else
    Result$ = KeyboardInkey()
    Code = Asc(Result$)
    If (Code > 32 And Code < 126) Or (Code > 160 And Code < 255)
      MessageRequester("Info", Result$)
    EndIf
  EndIf
ForEver
Quin
Enthusiast
Enthusiast
Posts: 279
Joined: Thu Mar 31, 2022 7:03 pm
Location: United States
Contact:

Re: Get all textual input (no matter locale) with KeyboardInkey()?

Post by Quin »

This did exactly what I wanted, thanks!
PB v5.40/6.10, Windows 10 64-bit.
16-core AMD Ryzen 9 5950X, 128 GB DDR5.
Post Reply