I was wondering if it was possible to program in JavaScript, something that was similar to how cell phones are able to scroll though a character map by the continuous pressing of the same key?
I was thinking of something like, holding down the [alt gr] key and have a change of letter state every time another key is pressed repeatedly. Most importantly, the specific alternative letters can be coded into the base letter.
ie. hold down [alt gr] + [standard “n” key]
= ṅ
…press n again
= ṇ
…press n again
= ñ
…
until [alt gr] is released.
Does anyone know how to do this, or could someone point me to a good tutorial about how JS deals with key presses?
Thanks.
An old butinstructive page about this «quite intricate thing» ![/QUOTE]
Before anything else, I must remind you that all the browsers have implemented key shortcuts, thus, for instance many combinations which use the ALT key will open some tool bar sub-menus (ALT+C, ALT+S, ALT+T,.... and so on...). So that, even if you might capture these combinations (which is, I said, a quite intricate thing across the browsers), you can not prevent their default behavior.[/QUOTE]
[CODE]<!DOCTYPE html>
<html><head><title>KeyBoard Input Test</title><meta charset="utf-8"><script type="text/javascript">
var alt_gr_bool = false; // checks whether the [alt gr] key is pressed or not
var array_position = 0; // monitors the array position
var foreign_array_A = ["u3041", "u30A1"]; // more to be added
document.onkeydown = enable_alt_gr;
document.onkeyup = reset_alt_gr;
function enable_alt_gr(e) { // What happens when [alt gr] is pressed.
if (e.keyCode == 0)
{
alt_gr_bool = true;
}
var KeyID = (window.event) ? event.keyCode : e.keyCode;
switch(KeyID) {
case 65: // The letter A/a
if (alt_gr_bool == true)
{
document.myForm.text_field.value = foreign_array_A[array_position];
e.preventDefault();
if (array_position < foreign_array_A.length - 1)
{
array_position ++;
}
else
{
array_position = 0;
}
}
break;
}
}
function reset_alt_gr(e) { // alt gr key is released. Everything back to normal.
if (e.keyCode == 0) {
alt_gr_bool = false;
array_position = 0;
}
}
</script></head><body>
<form name="myForm">
<textarea name="text_field" rows="10" cols="30"></textarea>
</form>
</body></html>
[/CODE]
I don't have ALTGR key. It is not a standard key. And I think that 80% of the users don't have it either (keep in mind that the special keyboards with the ALTGR key are rather rare). It is true that on a standard US-International keyboard layout (and similar) the CTRL+ALT could supply the ALTGR functionality. Not in your case. Your code does not work for me (with CTRL+ALT instead of ALTGR), neither in Chrome, nor in any other browser.[/QUOTE]
I must remind you that the Operating System (at least Window, which I commonly use) has the possibility to install different languages and a simple Language bar which can switch, at will, the meaning of some keys in order to return some special characters, characteristic of that foreign language. Of course, if the Font Family used permits that.
I am also using often that switch, as I am from Romania and our language, even if it is a Latin one, has several special characters. For instance, if I press the key "[" when my Language Bar is set to English, I get this: "[COLOR="Blue"][[/COLOR] ". When I switch to Romanian, I get this:"[COLOR="Blue"]î[/COLOR] ".
So: what would be the use of your toy? To emulate the special characters in the absence of an installed Language Bar? Interesting and ambitious aim. I don't say it is impossible, but could it be really useful? The Romanians who know that behind the "[COLOR="Blue"][[/COLOR] " key may be found the "[COLOR="Blue"]î[/COLOR] " character usually have installed the Romanian Language Bar on their computer, so that they might type that after a single click on the Language bar (or to use a shortcut - which can even be made as custom).[/QUOTE]
Maybe you are learning an unsupported language, (maybe a constructed language) or you might want to write some phonetic Latin (if it's not supported) for educational reasons.[/QUOTE]
Or: what about some buttons visible on the document. If your aim is to use form's elements like a text field or a textarea. Could be something like in the editor if this forum: when pressing a button, you can emulate a BOLD or ITALIC. This method is not so intricate, and it can be used to insert special characters. Except that you should have in the backstage a complete "dictionary". As I said, in Romanian language bar on, when I press [ I obtain î. You may simply capture the keys and, according to a switch button (on/off) you may replace the typed character with it's corresponded in that dictionary. I'll think about that, if I have some time... ?[/QUOTE]Yeah, this is a pretty good idea. It's a little like changing the language on the touch screen phones. If there are more than 2 characters mapped to one key though, it could get a bit tricky to implement, but I guess it would work really well on some languages.
Originally Posted by [B]Kor[/B]:
I don't have ALTGR key. It is not a standard key.[/QUOTE]
You are right ! It appears only on some keyboards, such as : US international, Scandinavian and Estonian, Croatian, Slovenian, Bosnian and Serbian (Latin), Latvian, Macedonian, German, French and Italian !
See[URL=]http://en.wikipedia.org/wiki/AltGr_key[/URL] ...[/QUOTE]
0.1.9 — BETA 5.9