[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Emacs keys differences between console and X



Andreas Tille writes:
 > 
 > I've done some key bindings in my .emacs (more detailed in an
 > *.el file called from .emacs but this is not the point):
 > 
 > (global-set-key [C-left]      'backward-word)
 > (global-set-key [C-right]     'forward-word)
 > (global-set-key [C-prior]     'beginning-of-buffer)
 > (global-set-key [C-next]      'end-of-buffer)
 > 
 > Well, this works fine under X :-). But under X the <Alt>-key
 > refuses to work as Meta. In any case I have to press <ESC>-... .

Check that your XF86Config file has this in it (in the 
Section "keyboard"):

    LeftAlt     Meta
    RightAlt    Meta


 > 
 > While working at the console my <Alt>-key works fina as Meta,
 > but the key bindings mentioned above don't work. <C-x> or any
 > <C-[a-z]> works fine but it seems me that the Control key didn't
 > work together with other special keys.  A hint for this
 > behaviour is that <C-h k><CTRL-LeftArrow> says that <left>
 > was pressed (the same when trying <C-h k><CTRL-F?> seems only
 > the F?-key to be pressed).
 > 

This is much harder.   Here's how to do it.  Basically we use
dumpkeys and loadkeys to specify what happens when you press
Control-left,  then we teach emacs about it.  


1. Use dumpkeys to dump the current key table

% dumpkeys > k

We'll copy that to k.orig to compare our changes after.

2.  Search in this file for "Left" and we find

keycode 105 = Left            
	alt	keycode 105 = Decr_Console    

Now, it would seem that all we have to do would be to add
	control keycode 105 = Control_left

But this doesn't work, because loadkeys doesn't know the symbol name
Control_left (or anything like it).  However, there are lots of "spare"
keysym names.  We'll start with F50.

So we make 2 changes.  Define control left to use string F50
	control keycode 105 = F50
and then tell it what characters to produce for F50.

But what should they be?  They could be anything you want,  even
something silly like "control left was pressed".  However, this
wouldn't be useful in emacs.  Since left generated the sequence
	Esc [ D
I tried using
	Esc [ C-d
(meaning escape, left square bracked, control D).  You want to ensure
that this isn't used by anything else, and isn't a prefix of an
existing emacs binding.  I was lucky in this choice.

So, we add this to file k:

# left is  Esc [ D,  so make  Control-left Esc [ C-d
string F50 = "\033[\004"

3.  Now use loadkeys to load these definitions in.
You'll have to be root to do this bit.

# loadkeys k
Loading k
#

4. Now, press C-h c Control-left in emacs.   It should say 

ESC [ C-d is undefined

That's good!  Now to teach emacs what it means.  We could do this:

(global-set-key "\M-[\C-d"   'backward-word)

which works,  but better would be to actually tell emacs that
Esc [ C-d  (or Meta-[ C-d as emacs prints it) is Control-left.
To do this:

(define-key function-key-map "\M-[\C-d"  '[C-left])

Now pressing C-h C-left should show:

C-left runs the command backward-word

It turns out that emacs already have backward-word bound to
Control-left;  otherwise we would have used

(global-set-key '[C-left] 'backward-word)


5.  Repeat for control-right.  Since right is  Esc [ D   we can
use Esc [ C-d.   Prior is   Esc [ 5 ~  so what should C-Prior
generate?  I tried   C-h c  Esc [ 5 #    and that that wasn't used so
I went with that,  and   Esc [ 6 #   for Control-next.a


So,  the differences for loadkey are:

% diff -c k.orig k
*** k.orig	Wed Apr 16 21:42:20 1997
--- k	Wed Apr 16 22:41:01 1997
***************
*** 223,236 ****
--- 223,243 ----
  keycode 103 = Up              
  keycode 104 = Prior           
  	shift	keycode 104 = Scroll_Backward 
+ 	control keycode 104 = F52
  keycode 105 = Left            
  	alt	keycode 105 = Decr_Console    
+ #	This doesn't work:
+ #	control keycode 105 = Control_left
+ #	So use F50
+ 	control keycode 105 = F50
  keycode 106 = Right           
  	alt	keycode 106 = Incr_Console    
+ 	control keycode 106 = F51
  keycode 107 = Select          
  keycode 108 = Down            
  keycode 109 = Next            
  	shift	keycode 109 = Scroll_Forward  
+ 	control keycode 109 = F53
  keycode 110 = Insert          
  keycode 111 = Remove          
  	control	alt	keycode 111 = Boot            
***************
*** 346,348 ****
--- 353,371 ----
  compose '"' 'y' to 'ÿ'
  compose 's' 'z' to 'ß'
  compose 'i' 'j' to 'ÿ'
+ 
+ # left is  Esc [ D,  so make  Control-left Esc [ C-d
+ string F50 = "\033[\004"
+ 
+ # similarly, make Control-Left  Esc [ C-c
+ string F51 = "\033[\003"
+ 
+ # F52:  Control-Prior
+ # prior is Esc [ 5 ~   Let's use Esc [ 5 # for it
+ string F52 = "\033[5#"
+ 
+ # F53:  Control-Next
+ # next is Esc [ 6 ~
+ string F53 = "\033[6#"
+ 
+ 
% 

And the required elisp code is:

(define-key function-key-map "\M-[\C-d"  '[C-left])
(define-key function-key-map "\M-[\C-c"  '[C-right])
(define-key function-key-map "\M-[5#" '[C-prior])
(define-key function-key-map "\M-[6#" '[C-next])
(global-set-key [C-prior]     'beginning-of-buffer)
(global-set-key [C-next]      'end-of-buffer)


--
TO UNSUBSCRIBE FROM THIS MAILING LIST: e-mail the word "unsubscribe" to
debian-user-request@lists.debian.org . 
Trouble?  e-mail to templin@bucknell.edu .


Reply to: