Include the ElkKeyboards jar file in your classpath and code your application in accordance with the following instructions.
In order to process keyboard maps in Java applications, the first step is to establish the list of .keylayout files that are available. The KeyCodeLanguages class provides this function. The constructor is called with the URL that points to the directory containing these files. The KeyCodeLanguages constructor finds the .keylayout files and the names of the languages to which they correspond. Next, you determine which language you want to use. The getKeyboardURL method asks the user for a selection and returns the URL to the appropriate .keylayout file. Having identified the .keylayout file, create a KeyboardHandler object that will be able to properly listen to and process user keystrokes. Finally, hook this handler to the JVM. The following code snippet illustrates this process.
// default path to .keylayout file folder (true would be for .ttf files)
URL url = null;
try { url = Status.getDefaultPath(false).toURI().toURL(); }
catch (Exception e) {}
// Find all the .keylayout files and associated languages
KeyLayoutLanguages dialog = new KeyLayoutLanguages(url);
// If .keylayout files exist, select one of the .keylayout entries
if (dialog.isKeyLayouts()) url = dialog.getKeyboardURL(this, icon);
// If no .keylayout files selected, exit program or add custom code
if (url==null) { System.exit(0); }
// Import the keyboard map and create a handler to process it
KeyboardHandler handler = new KeyboardHandler(url);
// Hook a KeyEventDispatcher to process the keyboard handler in the JVM
handler.setJavaHook();
The above snippet applies the keyboard map to all JTextComponent objects throughout the Java application. There is another alternative which is more selective. You might want to hook the keyboard to specific application components. This can be done. Delete the last statement in the above snippet and add the following whenever you want to connect a component to a keyboard map.
// Get available languages
String[] languages = dialog.getLanguages();
// Connect a component to the first language (Change index for the others)
// Note: If you have a handler object from a previous call, specify it in the third argument
KeyboardHandler handler = dialog.hookComponent(component, languages[0], null);
To unhook the keymap from the JVM use the instruction:
handler.releaseKeyboardHook();
To unhook the keymap from a single component, use the instruction:
dialog.unhookComponent(component, handler);Note: The unhook calls correspond to the method you use to hook. If you hook the handler to the JVM, use the first unhook procedure. On the other hand, if you hook handlers to specific components, use the second unhook approach.