getch()

This is a discussion on getch() within the C Programming forums, part of the General Programming Boards category; hello I've been trying to write a "getch()" function using ports but i gets this problem: when i call this ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    18

    getch()

    hello
    I've been trying to write a "getch()" function using ports but i gets this problem:
    when i call this function for the first time it works normally,
    i press somekey and the function prints the asciikey,but when i call it
    agin it prints the previous asciikey without any input from me!!!
    you get it?
    first time works normally.
    Second time prints the previous char and this is the code:

    unsigend char c='\0';
    while(c=='\0')
    {
    c=inportb(0x60);
    }
    switch(c)
    {
    case 0x1e:{putch('a');break;}
    case 0x03:{putch('b');break;}
    .
    .
    .
    .
    case 0x2c:{putch('z');break;}
    }

    so would you please help me
    and tell me what's wrong with this code

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Some keys send a message when they are released IIRC.

    edit:
    Code:
    void KeyboardIsr()
    {
       byte new_scan_code = inportb(0x60);
       // do something with the scancode ...
       // remember you only get ONE byte of the scancode each time the ISR is invoked
       // (though most of the times the scancode is only one byte)
    
       outportb(0x20,0x20);  // acknowledge the IRQ, pretty much tells the PIC that we can accept >=priority IRQs now
    }
    Apparently some keys give two bytes

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    18
    i tried this but didn't work i have been told i should clear the keyboard buffer
    how can i do that?

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Quote Originally Posted by abuashraf
    i tried this but didn't work i have been told i should clear the keyboard buffer
    how can i do that?
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    > I've been trying to write a "getch()" function using ports
    Do you have a 16-bit compiler (say turboc) running on a 16-bit OS (say DOS 6.0) ?

    Also, where are you going with this?
    Is it just for the hell of it, or are you trying to achieve something down the line?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 04:15 AM
  3. Pause a C program without getch()
    By swgh in forum C Programming
    Replies: 4
    Last Post: 02-20-2006, 10:24 AM
  4. Clearing input buffer after using getch()
    By milkydoo in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2003, 11:04 PM
  5. Problems with getch()
    By GrNxxDaY in forum C++ Programming
    Replies: 14
    Last Post: 08-12-2002, 02:11 AM

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