Jump to content

User:V111P/js/Simple Keyboard Layout Changer

From Wikipedia, the free encyclopedia
Simple Keyboard Layout Changer
DescriptionConverts the characters you type into other characters
Author(s)V111P
StatusBeta
UpdatedNovember 12, 2013; 10 years ago (2013-11-12)
BrowsersAll browsers
SkinsAll skins
SourceUser:V111P/SKLChanger.js

Simple Keyboard Layout Changer — with this script you can convert the characters you type in the search and resume input boxes and in the textarea into different characters — for example, into characters from a different language.

The script supports multiple layouts (multiple groups of characters to convert to) and you can switch between the different layouts with a click on a link. One link appears above the textarea and another one in the top right of the page (at least in the Monobook and Vector skins it's at the top right).

Warning: The way scripts that change the text in the textarea work is they overwrite the contents of the textarea with a new value on every change. In the case of SKLC this happens with every character you type (if it's a character that needs to be converted into another character). This affects how undo works, you can now undo only one character at a time (so now you need to press Ctrl-Z more). Test undo in your browser both with and without using this script to see the difference. In Google Chrome and Safari, undo doesn't appear to work at all after a script changes the contents of the textarea.

WikEd is also supported. If you turn WikEd on and off while editing, you may have to turn the SKLC off and on again to make it work. I could fix this, but probably won't unless somebody tells me they need it.

Installation

[edit]

To install the script, you need to edit your common.js (or skin.js). First, add the configuration part. Here is an example of setting one layout consisting of only three characters:

sKLChangerConfig = {
  defaultLayout: 'SY',
  charsToConvert: '^@`',
  layouts: [{
    // a short code name, such as EN, ES, etc, and a longer name
    name: 'SY', // a short, code name, such as EN, ES, etc.
    label: 'Symbols', // a longer name
    // characters
	chars: '№§€'
  }]
};

Include the defaultLayout: 'SY', line only if you want SKLC to be turned on and the (in this case) SY layout to be used by default. Without this you first need to click the link to activate the layout on every wiki page that you open.

Here's how the chars property of each layout is used when that layout is active: The first character in charsToConvert, when you type it, is going to be converted into the first character of the chars of the active layout, and so on. In the case of the example above, when the layout Symbols is activated, ^ will be converted into №, @ will be converted into §, and ` will be converted into €. Any other characters that you type won't be converted. Note that you need to escape \ and ' in the strings. Instead of a slash \ use two of them: \\ and instead of a single quote ' use a slash and a single quote after it: \'

You can also set the indicator's labels when turned off with the properties offName (the default value is OFF) and offLabel (the default value is The Keyboard Layout Changer is turned off). Set these just like you do the defaultLayout property.

Then, include this line in your common.js:

importScript('User:V111P/SKLChanger.js'); // [[User:V111P/SKLChanger.js]]

To use this script on a wiki other than the English Wikipedia, instead of importScript(...) use:

mw.loader.load('//en.wikipedia.org/w/index.php?title=User:V111P/SKLChanger.js&action=raw&ctype=text/javascript');

The default charsToConvert (that is, those used if you don't set the charsToConvert property of the sKLChangerConfig object) are:

'`-='
+ 'qwertyuiop[]\\'
+ 'asdfghjkl;\''
+ 'zxcvbnm,./'
// with Shift:
+ '~!@#$%^&*()_+'
+ 'QWERTYUIOP{}|'
+ 'ASDFGHJKL:"'
+ 'ZXCVBNM<>?';

One less important limitation of the application is that Caps Lock doesn't have any effect on character keys and its state can't be detected with JavaScript. If you want to use a letter on a character key, Caps Lock will not work there (of course, you can just use Shift).

Here is another example of setting sKLChangerConfig, this time with two layouts converting all character from the US standard keyboard other than the digits 0-9 into Cyrillic letters and other symbols:

sKLChangerConfig = {
  charsToConvert: // These are the characters, which are going to be converted when you type them
    '', // Use an empty string to use the default string with the characters in the US standard keyboard order
  layouts: [{
    name: 'BG', // a short, code name, such as EN, ES, etc.
    label: 'Bulgarian Standard', // a longer name
    chars: '`-.'
    + ',уеишщксдзц;('
    + 'ьяаожгтнвмч'
    + 'юйъэфхпрлб'
    + '~!?+"%=:/_№ІV'
    + 'ыУЕИШЩКСДЗЦ§)'
    + 'ЬЯАОЖГТНВМЧ'
    + 'ЮЙЪЭФХПРЛБ'
  },
  {
    name: 'BP',
    label: 'Bulgarian Phonetic',
    chars: 'ч-='
    + 'явертъуиопшщю'
    + 'асдфгхйкл;\''
    + 'зьцжбнм,./'
    + '~!@#$%^&*()_+'
    + 'ЯВЕРТЪУИОПШЩЮ'
    + 'АСДФГХЙКЛ:"'
    + 'ЗЬЦЖБНМ<>?'
  }]
};

You can, of course, type all the characters on one line if you prefer:
chars: '`-.,уеишщксдзц;(ьяаожгтнвмчюйъэфхпрлб~!?+"%=:/_№ІVыУЕИШЩКСДЗЦ§ЬЯАОЖГТНВМЧЮЙЪЭФХПРЛБ'

See also

[edit]