You are not logged in.

#1 2014-11-01 14:48:50

mahkoe
Member
Registered: 2011-10-28
Posts: 6

Tutorial: Virtual Numpad on laptops that don't have one built-in

Hi all! After getting so much good help from the people on Arch forums, I thought it was time to give back.

You remember how on older laptops, you would hit numLock and a portion of your keyboard would become a numpad? (specifically, the 789uiojklm keys) Well, recently I got a new Lenovo Y40 and I never knew how much I loved having a numpad until I found that my new laptop didn't have one. Unfortunately, the Y40 does not come with this feature out of the box, but luckily, Arch Linux is on our side. I am going to tell you how I set up my own virtual numpad using tools that come built-in with X.

Before we start, I should point out that everything I learned in order to do this was found here:
https://wiki.archlinux.org/index.php/X_ … _Modifiers
From the warnings section:

It is possible (and, in fact, easy) to end up disabling some keys on your keyboard for the current X session while working with XKB. This includes modifiers, cursor arrows and Enter key, as well as C-A-Backspace and C-A-Fx combinations. Make sure you have some way to terminate the session without using your keyboard.
While it is rare, changing XKB configuration can sometimes hang or crash X server. Make sure you can handle it. Having some way to killall X or reboot the host remotely may be a good idea.
Stop xxkb, or any other layout switching applications. xxkb actively changes XKB state. Debugging both at the same time is not a good idea.
And finally, be warned: it is very easy to get used to custom layout.

If you want to learn more, that wiki article is the best available xkbcomp documentation for people who aren't X developers (lit. me). And with that, let's get started!

Getting Set Up + xkb Background Info
The first thing we will do is run the command

xkbcomp $DISPLAY outfile.xkb

This will spit out the code that represents the current keyboard layout. Our end goal will be to code in what we need into this file, and upload it back to the X server. I recommend editing this file in an editor with C/C++-style syntax highlighting, since it might prevent you from making syntactical errors.

There are many sections to the .xkb file, but we will only concern ourselves with xkb_types, xkb_compatibility, and xkb_symbols.

  • xkb_types lets you define types for each of the keys on your keyboard. For example, if you use Canadian Multilingual Standard (like I do) most of your keys are given the type "EIGHT_LEVEL_SEMIALPHABETIC". When a "special key" like ctrl, alt, caps lock, etc is pressed, in xkb_types, we map these modifiers to "LevelN", and different combinations will mean different things for different types. For 8-level-semialphabetic, no special keys pressed is "Level1", Shift is "Level2", etc.

  • If we jump forward to xkb_symbols, and look at the keys, we see that each key is assigned a type, and an array. The first element in the array is what symbol the key represents for Level1, the second is the symbol for Level2, etc.

  • Finally, xkb_compatibility allows us to set which keys turn on which modifiers (as well as other things).

So with that, let's get started

Editing the layout

We will begin by editing the xkb_types section. First, we will pick an unused type from xkb_types (you could create your own, but it will be more work). It doesn't matter which type you picked, so long as none of your keys use it. Take down the name. Now delete the unused type, and in its place, copy paste the type that your keys used already. Finally, change the name of the copy to the type you deleted. For example, I deleted EIGHT_LEVEL_ALPHABETIC, copied and pasted EIGHT_LEVEL_SEMIALPHABETIC, and changed the name on the copy back to EIGHT_LEVEL_ALPHABETIC. Next, change the modifiers variable to accept NumLock. This is done by concatenating '+NumLock' before the semicolon. Lastly, we will bind it to a level. You can create a level, or simply pick one that you were not using, and add the line (where N is the desired Level)

 map[NumLock]= LevelN;

Next, we will turn our attention to xkb_compatibility. Simply copy paste this code into the xkb_compatibility section, taking care to replace Scroll_Lock with the key you wish to use to toggle the numpad:

    interpret Scroll_Lock {
        virtualModifier= NumLock;
        action= LockMods(modifiers=NumLock);
    };

Now comes the more tedious part of editing the layout. For every key that is part of your virtual numpad, change the type to the copy we made earlier. (In my example, I would change EIGHT_LEVEL_SEMIALPHABETIC to EIGHT_LEVEL_ALPHABETIC). Finally, locate the Nth element in the array (indexing from 1) where N is the level you bound the NumLock modifier to in xkb_types. Replace this with the desired nunmpad key. For your convenience, here is a list of the numpad keys I used:

KP_0
KP_1
KP_2
...
KP_9
KP_Add
KP_Subtract
KP_Divide
KP_Multiply
KP_Decimal

For example, here is the entry for my 'u' key (note the KP_4):

    key <AD07> {
        type= "EIGHT_LEVEL_ALPHABETIC",
        symbols[Group1]= [               u,               U,        NoSymbol,        NoSymbol,       downarrow,         uparrow,        NoSymbol,        KP_4 ]
    };

If you are wondering what to do if you want to bind numpad keys to your Fx keys, I added a note at the end of the post.

Uploading the layout to the X Server

And now the moment of truth. First, run

xkbcomp myNewLayout.xkb

This will NOT upload to the X Server yet; it will only compile the file and check its syntax. One cryptic error I got was because I used C-style /*block quotes*/. xkbcomp doesn't like these, but it doesn't mind //this kind of comment. If we have no errors, we can now upload with

xkbcomp myNewLayout.xkb $DISPLAY

Well, go ahead and try it out, there will almost surely be some really neat tweaks you can do.

Extra Stuff/Notes

When you hit Scroll_Lock (or whichever key it was you chose) it locks the new type we made to LevelN. This is why we went to the trouble of making a new type, so that the rest of the keyboard would not be locked in LevelN mode. For example, on my setup, I first tried this without creating a new type, and found that the rest of my keyboard was not behaving normally, since it was also bound to Level8 (my desired numpad level). So, I created a new type for my numpad keys so that the rest of the keyboard would still act in its usual manner even when I had the numpad on.

If you're wondering, I bound KP_Multiply and KP_Divide to my F8 and F9 keys. However, I didn't need to change the type, since the level I chose (Level4) also caused the Fx keys to act normally (except if I rebound them). That is, before I changed anything, all the Fx had the corresponding Fx key bound to Level4. I simply changed the Level4 keybinds for F8 and F9. This meant that when I activated the virtual numpad, F8 and F9 were KP_multiply and KP_Divide, and the rest of the function keys were what they were usually.

Hopefully, this tutorial won't only help you to make a virtual numpad, but maybe help some other newbies like me to further customize their system. And the Arch Wiki author is right, you really do get used to the new layout and it becomes frustrating to use other computers!

Happy trails.

- Marco

Last edited by mahkoe (2014-11-02 12:17:49)

Offline

Board footer

Powered by FluxBB