# Keep NumLock always on in Linux

## Intention

I intend to replace my own Bash script executing the following commands indefinitely in favor of this C++ compiled code, the reason being that the Bash script seemed very CPU ineffective.

It's not that I would think this solution is effective, nor perfect, but better than the one written in Bash for there is a great overhead over time.

The two commands are very simple:

• /usr/bin/numlockx on

• sleep 1

## Environment

Linux Mint 18 with Cinnamon desktop environment.

## Purpose

As I don't have a Num Lock indicator on my laptop, I want to ensure the Num Lock is always turned on.

## xmodmap

I have tried using this utility, but it literally, excuse my wording, crippled my key mappings, which I have set up from inside Cinnamon DE.

## Current C++ code

#include <cstdlib>  // in order to use system function
#include <unistd.h> // in order to use sleep  function

int main()
{

do {

system("/usr/bin/numlockx on");

sleep(1);

} while (true);

return 0;

}


This is compiled with g++ 5.x as follows:

g++ numlock-always-on.c -o "numlock-always-on"

• No offense, but you might be solving the wrong problem here. I assume the main purpose is to make sure NumLock is always on, but why? Are you encountering a problem, which possibly could be solved another way, you fixed with this program? – Mast Nov 22 '16 at 18:50
• @Mast I'd say having Numlock always on is a perfectly valid personal preference. – Simon Forsberg Nov 22 '16 at 18:58
• @SimonForsberg It's also a preference perfectly solvable by checking whether it's on the moment you need to use it and never turning it off. It's also settable at boot. I definitely smell an XY problem here. – Mast Nov 22 '16 at 19:13
• There are far better ways to keep numlock on than constantly running a program in the background. – esote Sep 27 '18 at 12:58

I don't know much C++, but your indentation isn't great. To fix this you could try running your code through a C/C++ linter.

Your code is also having a much larger performance hit than if you told your Xorg to do this. Assuming that you only want this to work in Xorg, rather than in both the console and Xorg, you can use setxkbmap as described in the Arch wiki page Keyboard configuration in Xorg. setxkbmap comes with a couple of options to change how your keyboard works, listed under /usr/share/X11/xkb/rules/base.lst. From this list numpad:mac looks like what you want, and will change your keyboard to always use numbers whether numlock is on or not.
And so I would use:

setxkbmap -option numpad:mac


Firstly, std::system is misspelt (you might get away with this on systems that also put <cstdlib>'s symbols into the global namespace, but that's not required of implementations).

Secondly, always check the return value from std::system(). If a user runs this without suitable DISPLAY and XAUTHORITY environment variables (e.g. after session end), it will run indefinitely, ignoring errors (and potentially filling up ~/.xsession-errors or equivalent with messages). So we probably want something like

while (std::system("/usr/bin/numlockx on") == 0) {
sleep(1);
}
// something went wrong
return 1;


Finally, there's no need to return 0; from main() - even less so when this point is never reached.