Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating AltGr replacement keys #106

Open
Frank13 opened this issue Sep 15, 2012 · 11 comments
Open

Creating AltGr replacement keys #106

Frank13 opened this issue Sep 15, 2012 · 11 comments

Comments

@Frank13
Copy link

Frank13 commented Sep 15, 2012

Hello Mottie,

Your keyboard is fantastic.
I have a question about replacement keys. Is it possible to create Alt Gr replacement keys? for instance I would like to create a keyboard to write in German with a French keyboard, so I would need only a few letters like the ß but I would like also to create a shortcut a bit like you have set up with the diacritic é = '+e. I would like to use an Alt Gr + s or Alt Gr + S. Is there a way to do it? I have tried replacement keys "\u00df(alt-b):bla_bla" or "\u00df(alt+b):bla_bla". It does not seem to work.

TIA

Kind regards
Frank

@Mottie
Copy link
Owner

Mottie commented Sep 16, 2012

Hi Frank!

If you alter the french-azerty-1 layout and replace the "alt-s" with \u00df(alt-s):bla_bla and then replace the "alt-shift-s" with \u00df(alt+S):bla_bla2 (added the 2 so you can tell there is a difference), it should work. Here is a demo.

$.keyboard.layouts['french-azerty-mod'] = {
    'default' : [
        "\u00b2 & \u00e9 \" ' ( - \u00e8 _ \u00e7 \u00e0 ) = {bksp}",
        "{tab} a z e r t y u i o p ^ $",
        "q s d f g h j k l m  \u00f9 * {enter}",
        "{shift} < w x c v b n , ; : ! {shift}",
        "{accept} {alt} {space} {alt} {cancel}"
    ],
    'shift' : [
        "{sp:1} 1 2 3 4 5 6 7 8 9 0 \u00b0 + {bksp}",
        "{tab} A Z E R T Y U I O P \u00a8 \u00a3",
        "Q S D F G H J K L M % \u00b5 {enter}",
        "{shift} > W X C V B N ? . / \u00a7 {shift}",
        "{accept} {alt} {space} {alt} {cancel}"
    ],
    'alt' : [
        "\u00b2 & ~ # { [ | ` \\ ^ @ ] } {bksp}",
        "{tab} a z \u20ac r t y u i o p ^ \u00a4",
        "q \u00df(alt-s):bla_bla d f g h j k l m  \u00f9 * {enter}",
        "{shift} < w x c v b n , ; : ! {shift}",
        "{accept} {alt} {space} {alt} {cancel}"
    ],
    'alt-shift' : [
        "{sp:1} 1 ~ # { [ | ` \\ ^ @ ] } {bksp}",
        "{tab} A Z \u20ac R T Y U I O P \u00a8 \u00a4",
        "Q \u00df(alt+S):bla_bla2 D F G H J K L M % \u00b5 {enter}",
        "{shift} > W X C V B N ? . / \u00a7 {shift}",
        "{accept} {alt} {space} {alt} {cancel}"
    ],
};

@Frank13
Copy link
Author

Frank13 commented Sep 17, 2012

Hi Rob,I have tested the demo on jsfiddle and I am afraid it does not work. When I press alt or AltGr I do see the correct keyboard with char \u00df but when I press Alt and s or alt and shift and S, no character isdisplayed in the input. I have tested all keys of the keyboard and only the e, 2, 3, 4 5 6 7 8 9 0 ° + £ keys are working these are the standard AltGr keys written on our keyboards. It seems thatall other are filtered?Do you have any other idea? Kind regardsFrank

@Frank13
Copy link
Author

Frank13 commented Sep 17, 2012

Hi Rob,I have analysed the problem a little further. I am using Chrome and keypress event is not triggered when I press AltGr + s. But KeyDown and KeyUp are fired with keycode (17 and then 18 for AltGr and 83 for s - tested in http://www.quirksmode.org/js/keys.html). This may be the reason why it is not working?Kind regardsFrank

@Mottie
Copy link
Owner

Mottie commented Sep 17, 2012

Actually the main reason why it isn't working is because Windows uses the Alt key to open the current application menu. If you are typing in a textarea/input and press Alt-F, the file menu will open. Now if you press the alt key a second time, the focus will return to the textarea/input.

So really the bottom line is that Ctrl may be a better meta key to use if you want to add shortcut key strokes, but just don't try to override the default editing tools & browser shortcuts (Ctrl + z, x, c, v, a, s, etc). I did try to ask a question on StackOverflow about keys to avoid... there is a more through list there, but no real answer.

@Frank13
Copy link
Author

Frank13 commented Sep 18, 2012

hello Rob

i am not sure that it is the main reason. on windows there is an alt key and an alt gr key. both are nearly the same except that alt is used for menu commands and alt gr is used for special characters and avoid conflicts between national keyboards and software commands (software are global even if localized)
both alt and alt gr activate the alt key of Mottie keyboard

it seems to me that Mottie keyboard is listening on the key press event for normal keys and not on key down and key up
which is not handled the same way by various browser

kind regards

Frank

@ZaDarkSide
Copy link

https://inexorabletash.github.io/polyfill/demos/keyboard.html
In this polyfill ALT+F does not trigger file menu to open, maybe you can use this somehow.

@Mottie
Copy link
Owner

Mottie commented Jun 5, 2015

Hi @ZaDarkSide!

Thanks for sharing, that is a really cool demo... I never knew you could differentiate left & right keys!

The main issue is if event.preventDefault() is added, the keyboard no longer adds the pressed key into the input/textarea.

@ZaDarkSide
Copy link

Another method is to trigger the event on the element you need and prevent the default:

    // with jQuery
    $(anotherel).on('keypress', function(e) {
        $(el).trigger(e);
        e.preventDefault();
    });

You can also try event.stopPropagation:

event.stopPropagation(); // this is standard

or event.cancelBubble

event.cancelBubble = true; // this is non-standard

@Mottie
Copy link
Owner

Mottie commented Jun 5, 2015

Sadly that doesn't seem to work. The keyboard needs to bind to "keyup", "keydown" and "keypress" events. If you prevent default on "keydown", the "keypress" event doesn't appear to fire, even if it is manually triggered.

@ZaDarkSide
Copy link

If you want to trigger the keypress or keydown event then all you have to do is:

var e = jQuery.Event("keydown");
e.which = 50; // # Some key code value
$("input").trigger(e);

http://stackoverflow.com/questions/832059/definitive-way-to-trigger-keypress-events-with-jquery

@Mottie
Copy link
Owner

Mottie commented Jun 7, 2015

In this case, I can't create a psuedo-event because the status of the shift, alt and meta key need to be known.

My facts may not be 100% accurate as I have't looked at this code in a while, so don't quote me on any of this information:

  • keydown & keyup - used for special key actions like tab, backspace, escape & preventing pasting. Using preventDefault here stops the "keypress" event from firing. Triggering it on the input does not make the character being typed appear!
  • keypress - provides more accurate keyCodes for typed characters (ref). Using preventDefault here does not stop special key actions.

I set up this demo to show you what happens when you use preventDefault on "keydown". Switching it to "keyup" seems to work better, but then again most of the stuff we want to prevent happens on "keydown".

Also, check out this madness.

@Mottie Mottie mentioned this issue May 5, 2017
28 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants