1

I'm looking for a way to redefine the row of numeric keys so that by default it will act as if I'm holding <shift> giving me the punctuation and symbol characters, and holding <shift> will give me the numbers. Basically reversing the behavior.

My issue is, I don't want the keypad keys to change.

For example:

  • pressing '4' up top would give $
  • pressing '4' on the keypad would give 4

I see how I could do the reverse: redefine the keypad keys, but the default behavior of the keypad numeric keys is to translate to the number, so redefining the number keys will affect both, right?

Does anyone know a way I could do this? Or maybe this has already been done somewhere?

  • 1
    I don't have a keyboard with a keypad in front of me, but you could try (global-set-key [kp-0] 'self-insert-command) etc, and then bind the "plain" numeric keys. I think Emacs would be able to distinguish the two, as long as you're not running it in a terminal. – legoscia Feb 27 '15 at 17:19
  • @legoscia this didn't work for me, but it helped suggest a direction that did. – A Schneider Feb 28 '15 at 19:01
  • If you found a solution, you can post it as an answer to your own question. It might turn out useful to someone who has the same question. – legoscia Feb 28 '15 at 19:58
4

After a bit of trial and error, I've come up with the following, somewhat crude implementation:

(defmacro ins-val (val)
  `(lambda () (interactive) (insert ,val)))

(define-minor-mode special-char-mode
  "Toggle Special Character mode"
  nil
  " SpecialChar"
  `(
    (,(kbd "1") . ,(ins-val "!")) (,(kbd "!") . ,(ins-val "1")) (,[kp-1] . ,(ins-val "1"))
    (,(kbd "2") . ,(ins-val "@")) (,(kbd "@") . ,(ins-val "2")) (,[kp-2] . ,(ins-val "2"))
    (,(kbd "3") . ,(ins-val "#")) (,(kbd "#") . ,(ins-val "3")) (,[kp-3] . ,(ins-val "3"))
    (,(kbd "4") . ,(ins-val "$")) (,(kbd "$") . ,(ins-val "4")) (,[kp-4] . ,(ins-val "4"))
    (,(kbd "5") . ,(ins-val "%")) (,(kbd "%") . ,(ins-val "5")) (,[kp-5] . ,(ins-val "5"))
    (,(kbd "6") . ,(ins-val "^")) (,(kbd "^") . ,(ins-val "6")) (,[kp-6] . ,(ins-val "6"))
    (,(kbd "7") . ,(ins-val "&")) (,(kbd "&") . ,(ins-val "7")) (,[kp-7] . ,(ins-val "7"))
    (,(kbd "8") . ,(ins-val "*")) (,(kbd "*") . ,(ins-val "8")) (,[kp-8] . ,(ins-val "8"))
    (,(kbd "9") . ,(ins-val "(")) (,(kbd "(") . ,(ins-val "9")) (,[kp-9] . ,(ins-val "9"))
    (,(kbd "0") . ,(ins-val ")")) (,(kbd ")") . ,(ins-val "0")) (,[kp-0] . ,(ins-val "0"))
    (,[kp-multiply] . ,(ins-val "*"))
    )
  :global 'true)

(global-set-key (kbd "C-!") 'special-char-mode)

Crude but it works. I'd welcome any improvements someone might suggest.

This global-set-key allows the user to toggle this mode on/off by pressing "C-!". I'm sure this particular set-key is bad practice but it makes nice intuitive sense to me.

| improve this answer | |
  • This should be packaged and distributed! – Hennadii Madan Jan 24 '17 at 8:50
  • Also compatible with evil-mode, i.e. this does not break normal "state", only affects the insert "state" – Hennadii Madan Jan 24 '17 at 9:05
  • Bug: numpad * is mapped to 8 – Hennadii Madan Jan 24 '17 at 9:24
  • OK, fixed by appending (,[kp-multiply] . ,(ins-val "*")) to line with "8". – Hennadii Madan Jan 24 '17 at 9:32
  • I started this, please let me know if you have any objections – Hennadii Madan Jan 24 '17 at 10:27
0

If you are under X you can use xmodmap to redefine your keys. i don't have numeric keyboard at hand, but quick web search says numerics send different codes. Here is my config:

keycode 10 = exclam 1
keycode 11 = at 2
keycode 12 = numbersign 3
keycode 13 = dollar 4
keycode 14 = percent 5
keycode 15 = asciicircum 6
keycode 16 = ampersand 7
keycode 17 = asterisk 8
keycode 18 = parenleft 9
keycode 19 = parenright 0

Unfortunately I don't have pure-emacs solution for that...

| improve this answer | |
  • I was planning to hook the key rebindings to a minor mode, so I could switch it on and off on the fly, although I did consider something like this. – A Schneider Feb 28 '15 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.