Meta Key

This is a discussion on Meta Key within the C Programming forums, part of the General Programming Boards category; I figured out a constant: #define CTRL 1-'a' That way, if I write: CTRL + 'a' is the ASCII value ...

  1. #1
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198

    Meta Key

    I figured out a constant:

    #define CTRL 1-'a'

    That way, if I write:

    CTRL + 'a'

    is the ASCII value of that key combination (CTRL here is that 1-'a' constant)

    This I figured out from those lower un-printable ASCII characters that are displayed with that '^' symbol and a letter.

    Emacs uses the Meta key for many commands (which is Alt on most keyboards), and I thought it would be cool if I can use that key in my programs.
    I wonder how to do that with the Meta/Alt key?
    Last edited by MTK; 09-19-2009 at 09:26 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by MTK View Post
    This I figured out from those lower un-printable ASCII characters that are displayed with that '^' symbol and a letter.
    A lot of the keys are three bytes, they usually start with ^[ ESC, int 27:
    Code:
    #include <stdio.h>
    
    int main () {
    	int c[3] = {0};
    	puts("Hit a key then enter.");
    	scanf("%c%c%c",&c[0],&c[1],&c[2]);
    	printf("%d %d %d",c[0],c[1],c[2]);
    	return 0;
    }
    That seems to include alt (meta) combinations, eg:

    Hit a key then enter.
    ^[^B /* alt-ctrl-b */
    27 2 10[root~/C] ./a.out
    Hit a key then enter.
    ^[b /* alt-b */
    27 98 10


    Which actually these are just two byte*, since the last one (10) was the newline. But PgUp is 3:

    Hit a key then enter.
    ^[[5~
    27 91 53


    You should look at the readline() library. That provides things for this and for command-line histories, tab completion, etc.

    * so I would guess the meta key is 27 (ESC).
    Last edited by MK27; 09-19-2009 at 09:55 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    I will try that, but I have an even bigger key problem (I am using ncurses). I have to be able to tell the difference between a newline and Ctrl+J, but the ascii value is the same!

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by MTK View Post
    I will try that, but I have an even bigger key problem (I am using ncurses). I have to be able to tell the difference between a newline and Ctrl+J, but the ascii value is the same!
    Oh yeah. Well, don't use ctrl-J (maybe there is a solution, but is it worth the time?)
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    How would you do it?

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by MTK View Post
    How would you do it?
    Okay, I thought this was because you wanted to assign ctrl-J a purpose! If you are just worried the user will serendipitously press ctrl-J and be surprised because it performs a carriage return, just put that in the docs ("Ctrl J = newline"). Big deal.

    If the user "serendipitously" dumps coffee on the keyboard, something else strange and unpredictable, essentially beyond your control, may happen. You do not have to take responsibility for everything (the OS's perogatives, hardware design, room temperature, etc). That will just contribute to an overblown, overweight app. You just have to be aware of them, deal with security issues (this is unlikely to be one), and make sure that, presuming reasonable use by a sane human being, everything functions in a sane and reasonable way. Failing to paranoidly "attend" to irrelevant details is not a sign that you are lazy or have not taken time with what is actually important (it may even be the opposite).

    I watch a few mailing lists for various apps; it is surprising how often someone will write in with something like "Bug: ctrl-J cause carriage return!" (who cares?) and the usually the developers will just explain why, and that this is not due to a programming error and hence is not a "bug". Then sometimes the glee club jump in and lambast this person for being so stupidly presumptuous as to believe that the developers of this fine application can be held responsible for a the ignorance of morons like that
    Last edited by MK27; 09-19-2009 at 12:32 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    No, that's not my point.

    My point is that I want the Ctrl+J and Enter keys to issue different commands in my ncurses program.
    Last edited by MTK; 09-19-2009 at 01:40 PM.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by MTK View Post
    No, I want the keyboard shortcut Ctrl+J do a special command in my ncurses program, other that type in a \n character.
    That was my point:

    Quote Originally Posted by MTK View Post
    Well, don't use ctrl-J (maybe there is a solution, but is it worth the time?)
    What's wrong with ctrl-j? [edit: same problem]

    I thought I had a solution, or a starting point to a solution, but I don't. This is an old perl test thing I found looking for that:
    Code:
    #!/usr/bin/perl -w
    use strict;
    # get hex values for use in regex \x
    # rearrange comments to get the raw character values
    
    use Term::ReadKey;
    
    ReadMode('cbreak');             
    print "Press a key.\n";
    
    my $buf = ''; 
    
    while (my $C = ReadKey()) {
            last if ($C eq "\n"); 
            $buf .= $C; #print "$C\t"; 
            my @hex = unpack("H*", $buf);
            print "@hex\t"; 
            $buf = ''; 
    }
    
    ReadMode('normal');  # IMPORTANT!
    This will crap out with ctrl-J too. Actually, it will with ctrl-j (the upper/lower case ctrl values come out the same).

    NB. with this I noticed those things I said were 3 byte keys are actually 4! But anyway, there you go, that is the Term::Readkey module (maybe standard, maybe needed...) from CPAN.

    This is a terminal issue; it is not this way in a GUI. The terminal interface (VT100 emulation or whatever*) is a venerable old (but near universal) thing, it is what it is. Ncurses has to work with that.

    I also notice ctrl-j in (non GUI) vim is a newline. If, after several decades of continuous development, Brian Moolenaar still has to accept this, I would guess you do too! There are like several hundred + keyboard macros in vim but ctrl-j is not one of them...alt-j works (join line).

    * essentially, this is the hardware. There is nothing you can do.
    Last edited by MK27; 09-19-2009 at 02:10 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Quote Originally Posted by MK27 View Post
    What's wrong with ctrl-j? [edit: same problem]
    You said that, not me.

    Quote Originally Posted by MK27 View Post
    * essentially, this is the hardware. There is nothing you can do.
    So basically you're telling me it's impossible.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by MTK View Post
    So basically you're telling me it's impossible.
    I can't say for sure MTK, but looking at the above evidence I would say 98% positive yeah, it's impossible. Just "one of those things". The original VT100 terminal was probably not a 101+ key keyboard, maybe that's why. 10==10. That's like you have two incoming messages on a socket: "WHOA" and "WHOA". How could you interpret them two different ways? Impossible.

    ps. the [edit] was my own observation...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    After trying a other layouts, I found that backspace and/or tab keep interfering!

    It is so nice with that meta key the way it uses a seperate byte. I wonder if it is possible for control to do that somehow.

    An interesting side effect of the fact the meta key prepends an ESC, is that you can press ESC and then the command key with the same effect as Meta+key!

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by MTK View Post
    An interesting side effect of the fact the meta key prepends an ESC, is that you can press ESC and then the command key with the same effect as Meta+key!
    Yes I told you that already:

    Quote Originally Posted by MK27
    A lot of the keys are three bytes, they usually start with ^[ ESC, int 27:

    That seems to include alt (meta) combinations, eg:

    Hit a key then enter.
    ^[^B /* alt-ctrl-b */
    27 2 10[root~/C] ./a.out
    Hit a key then enter.
    ^[b /* alt-b */
    27 98 10


    * so I would guess the meta key is 27 (ESC).
    Careful reading will save you time MTK :P
    Use alt-j...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    My idea was to have a set of movement command keys on the keyboard in a 3x2 formation under the right hand, and then with either Ctrl or Alt held down with the left hand will use different units.

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by MTK View Post
    My idea was to have a set of movement command keys on the keyboard in a 3x2 formation under the right hand, and then with either Ctrl or Alt held down with the left hand will use different units.
    I use alot of one handed alt-ctl patterns on the left in fvwm (a,s,z,x) because my arrows, home, etc are all on the right, and ctl-pgup etc is very common, so this keeps my forearm muscles symmetrical, like a pianist

    Personal opinion.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Those keys are also reserved for other purposes in my program.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 09:00 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21