Keyboard layout.js

From Spheriki

Jump to: navigation, search

The custom script keyboard_layout.js by Sulphur21 provides a replacement for the Sphere function GetKeyString(), which is usually used, when string input is to be implemented into a Sphere project. Other than the latter, which is restricted to the US keyboard layout, keyboard_layout.js helps implementing support for various international keyboard layouts.


Contents

About keyboard layouts in general

Keyboard layouts usually differ in the way they associate characters with keys. Depending on the language a layout is meant for, it might provide easy access to certain language-specific special characters (like accented characters or umlauts).

Additionally, most layouts have an "Alternate Graphic" key (AltGr), which can be used in combination with other keys to produce special characters. Pressing AltGr is the same as pressing Ctrl and Alt at once.

Another specialty of some keyboard layouts are "dead keys". A dead key itself is not meant to represent a character. Instead it modifies the character which is associated with the next pressed key. For example, pressing the "´" key on some keyboards will produce no immediate result. But if "a" is pressed afterwards, it turns into "á".


Download and usage

Download keyboard_layout.js.txt, rename the file to keyboard_layout.js and copy it into the scripts folder of your project. The license of the script has been placed on top of the script code. Read it first. Then use:

 RequireScript("keyboard_layout.js");


Variables and functions of keyboard_layout.js

GetKeyStr(key, shift, altGr, keyboard_layout);

This function is pretty similar to the Sphere standard function GetKeyString(). It returns the char associated with the keycode 'key' in accordance with the given keyboard layout. Control keys return an empty string. Dead keys return the same, but will modify the char that is returned when GetKeyStr() is called the next time. If the next char is not modifiable by the dead key, the default char of the dead key PLUS the char of the pressed key will be returned (ergo the function will return a string of length=2).

These are the function parameters:

  • 'key': The keycode (as it is returned by GetKey() for example).
  • 'shift' and 'altGr': Booleans that represent the state of the Shift and the AltGr key. If 'shift' is true the uppercase character associated with a key will be returned. If 'altGr' is true, the special character of that key is returned. If both are true the "uppercase special character" of the key is returned (not all keyboard layouts have this though).
  • 'keyboard_layout': A string defining the keyboard layout. If it is not specified, it defaults to "US", which will make GetKeyStr() work quite exactly like GetKeyString(). Currently, these layouts are supported:
    • "Germany" (and Austria)
    • "Netherlands"
    • "UK" (and Ireland)
    • "US" (and most other English-speaking countries)
    • "US-International"


Example

The example will create a very basic input line (without a cursor or even the chance to delete something you've entered):

 RequireScript("keyboard_layout.js");
 var Shift, AltGr, KeyCode, Text = "", Font = GetSystemFont();
 function game() {
   while (!IsKeyPressed(KEY_ENTER)) {
     Font.drawText(0,0, "Enter some text: " + Text); 
     FlipScreen();
     Shift = IsKeyPressed(KEY_SHIFT);
     AltGr = IsKeyPressed(KEY_CTRL) && IsKeyPressed(KEY_ALT);
     KeyCode = GetKey();
     Text += GetKeyStr(KeyCode, Shift, AltGr, "Germany");
   }
 }

Note that the key state of AltGr is determined by checking wether Ctrl and Alt are both pressed.


Adding more keyboard layouts

Want to add more layouts to the script? Or remove one? Or repair a buggy one? '^_^

If you know what you're doing: have a look at the properties of the KeybLayoutData object. Each represents a layout. The numbers are keycodes. The arrays assigned to them contain the associated chars in this order: [0] the lowercase char of the key, [1] the uppercase char of the key, [2] the special (AltGr) char of the key and [3] the uppercase special char (AltGr+Shift) of the key. Chars that aren't specified default to "". If the array of a key contains an object, the respective char behaves as a dead key. The object contains the keycodes and chars of the keys that are changed by it. The defaultChar is used if the dead key is not followed by one of these.


Notes

Remember, that it doesn't make much sense to choose a keyboard layout that supports characters that aren't properly represented by the font you're using.


External links

Personal tools