Capturing the UP Arrow Key


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Capturing the UP Arrow Key

  1. #1
    Join Date
    Apr 2009
    Location
    Singapore
    Posts
    13

    Unhappy Capturing the UP Arrow Key

    Dear all,

    Background: Rabbit Microcontroller.
    Accessing the board via Telnet. TCP/IP
    Uses VT100 Emulation Telnet.

    I had been thinking how do I check whether a user had keyed in an UP ARROW and bring up his previously entered command. I tried to do a HEX key capture and they returned me 3 HEX numbers.

    I am using a Switch Case, I can't combine these 3 HEX codes together and match the UP ARROW as one case.

    Each of my character is received from a buffer and not captured as an input string stream.

    Meaning, one char by one char it goes into a char variable read from the buffer.

    HEX KEY for UP arrow is 1b 5b 41

    I checked up the net and they gave me 1E as UP ARROW but funny thing is it can't work!

    Examples:

    (Can't work)
    case '\x1E':
    do somthing...
    break;

    (Compile error)
    switch(ch)
    {
    case '\x1b\x5b\x41'
    Do something
    }

    thanks for reading. I am using Dynamic C compiler, its not those normal ANSI compiler I guess. Its from the microcontroller manufacturer www.rabbit.com

  2. #2
    Join Date
    Dec 2007
    Posts
    401
    > HEX KEY for UP arrow is 1b 5b 41

    right. these are three different bytes: 0x1b, 0x5b, 0x41
    these may be followed by one or more null bytes (which you can ignore).

    that means: if the character read from the buffer is 0x1b, it is the escape (ESC) character. you need to read two more characters from the buffer to interpret the keyboard code. something like:

    Code:
     switch(ch)
    {
         // ...
    
         case 0x1b : // this is an escape sequence
         {
                char second = read_next_char_from_buffer() ;
                switch(second)
                {
                           // ...
     
                          case 0x5b: //  '[' this is an arrow key
                          {
                                 char third = read_next_char_from_buffer() ;
                                 switch(third)
                                {
                                          case 0x41: //  'A' : this is an up arrow key
                                              // process it
                                              break ;
                                          case 0x42: //  'B' : this is a down arrow key
                                              // process it
                                              break ;
                                         
                                           // etc
                                }
                          }
    
                           // ...
                }
         }
    
         // ...
    }
    for VT100 keyboard codes see: http://www.nw.com/nw/WWW/products/wizcon/vt100.html

  3. #3
    Join Date
    Apr 2009
    Location
    Singapore
    Posts
    13

    Thumbs up HEX KEY for UP arrow is 1b 5b 41

    Thanks so much! I will give it a try...

  4. #4
    Join Date
    Apr 2009
    Location
    Singapore
    Posts
    13

    Smile

    Dear Vijayan,

    Thanks so much for your detailed help. It's working now.

    Dav

    Quote Originally Posted by vijayan View Post
    > HEX KEY for UP arrow is 1b 5b 41

    right. these are three different bytes: 0x1b, 0x5b, 0x41
    these may be followed by one or more null bytes (which you can ignore).

    that means: if the character read from the buffer is 0x1b, it is the escape (ESC) character. you need to read two more characters from the buffer to interpret the keyboard code. something like:

    Code:
     switch(ch)
    {
         // ...
    
         case 0x1b : // this is an escape sequence
         {
                char second = read_next_char_from_buffer() ;
                switch(second)
                {
                           // ...
     
                          case 0x5b: //  '[' this is an arrow key
                          {
                                 char third = read_next_char_from_buffer() ;
                                 switch(third)
                                {
                                          case 0x41: //  'A' : this is an up arrow key
                                              // process it
                                              break ;
                                          case 0x42: //  'B' : this is a down arrow key
                                              // process it
                                              break ;
                                         
                                           // etc
                                }
                          }
    
                           // ...
                }
         }
    
         // ...
    }
    for VT100 keyboard codes see: http://www.nw.com/nw/WWW/products/wizcon/vt100.html

  5. #5
    Join Date
    Apr 2009
    Location
    Singapore
    Posts
    13

    Question

    Dear Vijayan,

    I had tried using the coding sample you given. However, when I pressed ESC button and capital B, it still executes my code. I just used a testing code to print out "This is UP arrow", "this is DOWN arrow".

    I tried some ways to overcome this "bug" but wasn't successful.

    Code:
    case '\x1b': 
    				{
    					ch = '\0';
    					ch2 = bufRx[i];
    						switch(ch2)
    							case '\x5b':
    								ch2 = '\0';
    								ch3 = bufRx[i];
    									switch(ch3)
    										case '\x41':
    											tnPrintf("This is UP key");
    											break;
    										case '\x42':
    											tnPrintf("This is DOWN key");
    											break;
    					//Simulate a UP key? History comes up..
    				}

  6. #6
    Join Date
    Dec 2007
    Posts
    401
    this is the intended behaviour. how else would you type an UP-ARROW on a keyboard that did not have arrow keys?

    the original meaning of the escape character (ESC) is: the following characters should be treated in a special way - they identify some special function to be performed. ( ESC has been backronymed by some to stand for "Extra Services Control".)

    VT-100 (and ISO/IEC 6429 which is based on the VT-100 specifications) defines the two-character sequence ESC+'[' as the Control Sequence Introducer or CSI - a prefix affecting the interpretation of some number of contiguous characters following it.

    however, we have ended up with a key labeled "Esc" on the keyboard - and it transmits the ESC character. windows users expect this to cancel the current task rather than prefix a character sequence indicating a special function. Unix programs such as vi, use it to terminate input mode and return to command mode.

    with this dual use of ESC, only timing can distinguish weather the user typed the three keys ESC+'['+'A' ( intent: up-arrow ) or intended just an ESC by itself. for example, in BSD, the os waits for a short (configurable) period of time after receiving an ESC to see if more characters follow it immediately.

  7. #7
    Join Date
    Apr 2009
    Location
    Singapore
    Posts
    13

    Smile Up arrow captured solved

    Hey guys,
    thanks for helping. Esepcially Vijayan.
    I had managed to modify the code to work for my Microcontroller.

    Code:
    case '\x1b': //ESC pressed, so we should do something? 
    				{
    					tnPrintf("\r"); //Don't do anything when ESC key presssed.
    					ch2 = bufRx[1];
    					//tnPrintf("%s",ch2);
    						switch(ch2)
    							case '\x5b':
    								{
    								ch3 = bufRx[2];
    								//tnPrintf("%s",ch3);
    								switch(ch3)
    									{
    										case '\x41': //ESC key does not come here.
    										{
    											tnPrintf("This is UP key");
    											tnPrintf("\r");
    											break;
    										}
    										case '\x42':
    										{	//tnPrintf("Case 3a: %x", ch);
    											tnPrintf("This is DOWN key");
    											break;
    										}
    									}
    								}
    					//Simulate a UP key? History comes up..
    				}

Similar Threads

  1. Replies: 7
    Last Post: 05-30-2008, 08:33 AM
  2. Help: Shorcut key for a commandbutton
    By craver06 in forum VB Classic
    Replies: 10
    Last Post: 02-06-2008, 12:12 PM
  3. Replies: 0
    Last Post: 09-21-2006, 08:19 AM
  4. Arrow Key Input
    By Runesoul in forum Java
    Replies: 4
    Last Post: 05-12-2005, 09:20 PM
  5. Capturing ESC key in DataReport window
    By Jeff Ross in forum VB Classic
    Replies: 2
    Last Post: 06-13-2001, 08:47 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center