Guest User

Untitled

a guest
Feb 7th, 2021
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var _caps : bool = false
  2.  
  3. func _ready():
  4.     # Godot doesn't have any built in way of checking the status of the caps lock key.
  5.     # This short demonstration will show you a work around.
  6.     # This is an inelegant solution but it seems to work under Windows 10 and Pop_OS! 20.04
  7.     # I would recommend only calling this once when your game starts, after which you can track the changes to the capslock key inside of godot
  8.     # This still needs testing on MacOS, older versions of windows and other linux flavours
  9.    
  10.     var output = [] # Output will contain the output of the execute function
  11.        
  12.     if (OS.get_name() == "X11"): # Does this completely fail on Wayland? Maybe it will be better to use a solution similar to windows?
  13.         var err = OS.execute("bash", ["-c", "xset -q | grep Caps"], true, output)  # bash calls /bin/bash a command interpreter
  14.         if (err == OK):                                                          # xset -q gets the status of the keyboard
  15.                                                                                  # | grep Caps outputs the line that starts with Caps
  16.                                                                                  # true pauses the game until the command finishes
  17.                                                                                  # output, see above
  18.             var result = output[0].split(":")
  19.             if (result[2].find("on") != -1):
  20.                 _caps = true
  21.         else:
  22.             print ("Sorry something went wrong. Maybe you don't have bash or xset. Godot Error: " + str(err))
  23.     elif (OS.get_name() == "Windows" or OS.get_name() == "UWP"):
  24.         var err = OS.execute("capsChecker.exe", [], true, output) # This runs a small program written in c++, see capsChecker.cpp
  25.         if (err == OK):
  26.             if (output[0] == "On"):
  27.                 _caps = true
  28.         else:
  29.             print ("Sorry something went wrong. Please make sure capsChecker.exe is present in res://. Godot Error: " + str(err))
  30.     elif (OS.get_name() == "MacOS"):
  31.         pass # todo probably need to call a different executive
  32.     else:
  33.         print("You are likely on mobile and the virtual keyboard sorts all this out for you.")
Advertisement
Add Comment
Please, Sign In to add comment