Thread: Will using this code cause problems? reading keyboard...

  1. #1
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350

    Will using this code cause problems? reading keyboard...

    I seek the advice of the professional programmers yet again. I'm working on a bit of a text editor. Since I've never taken any data structures classes, nor have I worked with massive amounts of data, I like to know if this sort of coding would cause random errors or other undesired results.

    Code:
    unsigned key1, key2;
    
    key1 = getch();
    
    if(key1 == 0)
    {
          key2 = getch();
          switch(key2)
          {
                /* process whatever special key */
          }
    }
    
    else
    {
         key2 = 0;  /* just in case */
         key1 = (char)key1;
    
         putchar(key1) /* echo the input at current cursor pos */
    }
    I tried that _bios_keybrd stuff, but it took way too much code and was too cumbersome to deal with. Then I found this sort of thing doing a search on the board. I tried it and liked the results. Any comments would be greatly appreciated.

    TIA
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What are you trying to do actually with this bit of code?

    Here:

    if(key1 == 0)

    This is the decimal value of zero, not the character that represents zero. Is this what you want?

    key1 = (char)key1;

    putchar(key1) /* echo the input at current cursor pos */


    There is no point in doing the first of those two lines. You can do the same thing in a single line:

    putchar( (char) key1 );

    Incidently, 'putchar' takes an integer as its argument.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Originally posted by quzah
    What are you trying to do actually with this bit of code?

    Here:

    if(key1 == 0)

    This is the decimal value of zero, not the character that represents zero. Is this what you want?

    I am after the decimal value. I need to scan for keys like function keys, arrow keys etc. I believe getch() will return 0 when a special key is pressed followed by that key's scan code. Is this not right?

    Say the user presses the up arrow key. key1 will have the 0 and key2 will get the 72. I'm looking to find a way to process the keyboard without having to use the bios or int 16h directly



    key1 = (char)key1;

    putchar(key1) /* echo the input at current cursor pos */


    There is no point in doing the first of those two lines. You can do the same thing in a single line:

    putchar( (char) key1 );

    Incidently, 'putchar' takes an integer as its argument.

    Quzah.
    Thanks for that condensed form of putchar, but I posted that in a mad rush before my login timed out :P

    The goal in this madness is to read the scan code. I'll convert to printable chars later in the process, and if there is a special key in key2, then I'll process the particular key in a function. This is for a dos program. No win api stuff for me. : )

    Thanks for the reply quzah.
    Last edited by ronin; 08-16-2002 at 07:51 PM.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you enable cookies in your login options you won't time out any more.

    Quzah. (busy at work right now)
    Hope is the first step on the road to disappointment.

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350


    I'm too paranoid for that.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help for reading two keyboard inputs
    By Diablo02 in forum C# Programming
    Replies: 12
    Last Post: 12-06-2007, 02:21 PM
  2. Replies: 6
    Last Post: 05-12-2005, 03:39 AM
  3. Reading from file problems
    By highlands in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2004, 12:50 PM
  4. Replies: 0
    Last Post: 02-21-2002, 06:05 PM
  5. File reading code - beyond help.
    By Brian in forum C Programming
    Replies: 3
    Last Post: 01-31-2002, 12:28 PM