Table of contents


Metro 4 lets you easily add handlers for keyboard events anywhere in your site.

This feature works for desktop browsers.


To bind hotkey add attribute data-hotkey with hotkey value to your control.

                    <ul class="v-menu">
                            <a href="#"
                                onclick="alert('Ctrl+1 is clicked')"
                                data-hotkey="Ctrl+1">Press Ctrl+1</a>

                    <button class="button"
                            onclick="alert('Alt+2 clicked!')">Press Alt+2</button>

                    <a href="#"
                            onclick="alert('Shift+3 clicked!')">Press Shift+3</a>

Modifiers are not case sensitive (Ctrl == ctrl == cTRL). If you want to use more than one modifier (e.g. alt+ctrl+z) you should define them by an alphabetical order e.g. alt+ctrl+shift. Hotkeys aren't tracked if you're inside of an input element (unless you explicitly bind the hotkey directly to the input). This helps to avoid conflict with normal user typing.

                    <a href="#"
                            onclick="alert('alt+ctrl+shift+z clicked!')">Press alt+ctrl+shift+z</a>

Hotkeys aren't tracked if the user is focused within an input element or any element that has contenteditable="true" unless you bind the hotkey directly to the element. This helps to avoid conflict with normal user typing. If you don't want this, you can change the booleans of the following to suit before metro.js loaded:


                        METRO_HOTKEYS_FILTER_CONTENT_EDITABLE = false;
                    <script src="metro/js/metro.js"></script>


In Metro 4 hotkeys triggered when keyup fired. To bubble up your hotkey event set METRO_HOTKEYS_BUBBLE_UP to true before metro.js loaded.


Firefox is the most liberal one in the manner of letting you capture all short-cuts even those that are built-in in the browser such as Ctrl-t for new tab, or Ctrl-a for selecting all text. You can always bubble them up to the browser by returning true in your handler.

Others, (IE) either let you handle built-in short-cuts, but will add their functionality after your code has executed. Or (Opera/Safari) will not pass those events to the DOM at all.

So, if you bind Ctrl-Q or Alt-F4 and your Safari/Opera window is closed don't be surprised.

Custom binding

For custom hotkeys you can use next supported event types: keydown, keyup and keypress. To create own event:

                    $(expression).bind(types, keys, handler);
                    $(expression).unbind(types, handler);

                    $(document).bind('keydown', 'ctrl+a', fn);

                    // e.g. replace '$' sign with 'EUR'
                    $('').bind('keyup', '$', function(){
                      this.value = this.value.replace('$', 'EUR');

                    $(expression).on(types, null, keys, handler);
                    $(expression).off(types, handler);

                    $(document).on('keydown', null, 'ctrl+a', fn);

                    // e.g. replace '$' sign with 'EUR'
                    $('').on('keyup', null, '$', function(){
                      this.value = this.value.replace('$', 'EUR');