Files

125 lines
7.5 KiB
Plaintext
Raw Permalink Normal View History

Episode: 2024
Title: HPR2024: Remapping Keys with xmodmap
Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr2024/hpr2024.mp3
Transcribed: 2025-10-18 13:27:41
---
This is HPR episode 2020 for Entitled, remapping keys with X Modmap.
It is hosted by John Culp and in about 9 minutes long.
The summary is, I describe how I use X Modmap to remap my spacebar to make underscores.
This episode of HPR is brought to you by an Honesthost.com.
Get 15% discount on all shared hosting with the offer code HPR15.
That's HPR15.
Better web hosting that's Honest and Fair at An Honesthost.com.
Hey everybody, this is John Culp in Lafayette, Louisiana, finally recording another episode.
Taking a few minutes before work starts this morning to try and get a quick one done.
This is a topic that I've had on my to-do list for quite a while.
And it's about remapping keys on your computer so that the key you press actually performs
another function.
And the idea for this came, I don't know, maybe a year or more ago when I was driving around
with my son.
And we were having one of those father-son discussions that happened a lot.
We were talking about file names.
And I was complaining about how my colleagues are always sending me files with spaces in
the file name and how that is, it's not ideal on Unix because of the way command line
parameters are processed and stuff.
And so in the midst of this rant about file names and spaces and file names, my son had
an idea.
He said, well, what about if whenever someone went to save a file, it changed your keyboard
so that when you press the space bar, it would make an underscore instead of a space.
And I thought, huh, that sounds like a pretty good idea.
And I decided to start seeing if I could make that happen.
Now ideally, the way that would work is that whenever a save dialog box is opened up,
the keyboard is remapped so that the space bar makes underscores.
But I don't know enough about the way a computer works to be able to create some kind of
daemon that is always checking to see whether there's a save dialog box or whether you
can create something that as soon as a save dialog box opens up, it triggers some other
event to remap the keys.
But I did a little bit of research about remapping keys and found two, well, the one tool that
does the remapping is called X Mod Map, that's X-M-O-D-M-A-P.
And you can use that command line tool to remap the keys on your keyboard.
I think it also does other things like maybe it changes mouse button functions if you
want it to and stuff like that.
But all I wanted to do was to change the mapping of the space bar.
And so I started looking at the documentation and then realized that I'm going to, there's
a little bit of information I need before I can make it work.
I needed to find out what the key code of the space bar was.
And to find that, there's another command line tool you can use called X-E-V, I guess
it's short for X-Event, X-E-V.
So anyway, if you run X-E-V, it starts putting a bunch of information in your terminal every
time some kind of X-Event happens, you get a bunch of terminal output telling you all
kinds of information about it.
So I started X-E-V and then I hit the space bar and checked the output from X-E-V after
hitting the space bar and I got a whole, let's see, about six lines of text starting with
the words key press event.
And the most important, most salient bit of information there is the key code on the third
line, second column over if you're doing like comma delimited columns.
It says key code 65.
So that tells me that my space bar has the key code of 65.
So then to remap that key to make an underscore, I would run the following command.
This is X mod map, space dash E, space, open quote, key code space 65, space equals space
underscore closed quote.
After running that command, every time I hit the space bar, it makes an underscore character
instead of a space.
And you can test it out also by running, if X-E-V is still running, then you can just hit
the space bar again and then you get a bit of different information.
It says key code 65 makes an underscore, yeah, I mean it actually says it in the keyboard
output, sorry, in the X-E-V output.
And then if you want to switch it back, then you run the command X mod map, space dash
E, space, open quote, key code space 65, space equals space.
And then the word space, closed quote.
And so of course it would be very tedious to have to actually type out these commands
at a terminal whenever you want it to be able to remap the space bar.
And so what I did was, what I normally do, and that's to make a bladder command that
will launch this for me.
So if I'm typing in a file name, I can just speak my bladder command, which is, I made
a bladder command that says make underscores.
And then the command to be executed is the very first one I read to you that changes key
code 65 to underscore.
So when I speak the command make underscores, it launches that command in the background
and instantly my space bar is mapped to the underscore key.
So I can type in a file name and then just like put spaces to my heart's content only
they're not spaces, they're underscores.
And then when I'm done saving the file, I don't want to keep making underscores with my
space bar.
So I say another bladder command, make spaces.
And that will put the key mapping back the way it was before.
So I don't know, this is kind of a hacked up way to achieve what my son had envisioned.
And I would love for somebody to make a follow-up episode to this.
Anyone who knows better how to monitor system processes and stuff like that, if you could
figure out a way to have these commands launched automatically, either when a save dialog
box has just been opened or when it has just been closed, that would be amazing.
Got a couple of links in the show notes.
One is to the X Mod Map Man page, the other is to the X EV Man page.
So you can learn more about those commands if you want.
I don't intend this episode to be any kind of exhaustive description of what these commands
do.
I'm sure they do way more than what I'm saying here.
I was just trying to achieve one thing.
I also have a video demonstration of this thing in action.
It's funny.
When I was, I had this topic on my list to do on HPR, but I kept thinking it seems like
I've done this before.
And then I remembered I had made a video demonstration very shortly after I figured out how to do it.
And that's why it seemed to me that I had talked about these things before, because I'm
of course talking while I'm demonstrating in a screencast.
But anyway, the video is at the bottom of the show notes.
There's a YouTube Embedded video there.
So go watch if you're interested and go have some fun with command line tools.
And of course, also please record episodes for Hacker Public Radio.
Thanks.
This has been John Colp and Lafayette Louisiana.
Bye y'all.
You've been listening to Hacker Public Radio at Hacker Public Radio dot org.
We are a community podcast network that releases shows every weekday Monday through Friday.
Today's show, like all our shows, was contributed by an HPR listener like yourself.
If you ever thought of recording a podcast, then click on our contributing to find out
how easy it really is.
Hacker Public Radio was founded by the digital dot org pound and the Infonomicom Computer
Club, and is part of the binary revolution at binwreff.com.
If you have comments on today's show, please email the host directly, leave a comment on
the website or record a follow-up episode yourself.
Unless otherwise stated, today's show is released under Creative Commons, Attribution,
Share a Like, free dot org license.