Thread: Yet antoher keyboard/graphics question

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    103

    Yet antoher keyboard/graphics question

    I know that with only C, you cannot interface with the keyboard or monitor but you can with assembly. I was wondering if just to recieve input from the say the keyboard (not parse it or anything, just recieve)would 300+ lines of code or was it easier than that? If it is a reasonable amount, then can someone post the code or direct me toward a link?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > I know that with only C, you cannot interface with the keyboard or monitor but you can with assembly
    What's that supposed to mean?
    Sure you can do those things in C, just not ANSI-C.

    How close you can get to the keyboard depends on your OS.

    You need to say more about your OS and Compiler for a more complete answer.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Poincare View Post
    I know that with only C, you cannot interface with the keyboard or monitor but you can with assembly. I was wondering if just to recieve input from the say the keyboard (not parse it or anything, just recieve)would 300+ lines of code or was it easier than that? If it is a reasonable amount, then can someone post the code or direct me toward a link?
    You can use C, and you should use C. You just can't use *PURE as the driven snow*, standard C.

    Just find a compiler that includes (or can be arm-twisted), into using kbhit(). I use legacy Borland Turbo C/C++ ver. 1.01 for this, (using just the C compiler, not the C++ compiler), but a library like Ncurses may handle it, as well as other compilers.

    Code:
    /*
    kbhit   Checks for currently-available keystrokes.
    
     Syntax:
       int kbhit(void);
    
     Prototype in:
     conio.h
    
     Remarks:
    kbhit checks to see if a keystroke is currently available.
    
    Any available keystrokes can be retrieved with getch or getche.
    
     Return Value:
    If a keystroke is available, kbhit returns a nonzero integer; if not, it
    returns 0.
    
     Portability:
    kbhit is unique to DOS. (Works fine in Windows XP, however)
    
     See Also:
      getch    getche
    
     Example:
    */
    #include <conio.h>
    #include <stdio.h>
    
     int main(void)
     {
        int key1;
        cprintf("Press any key to continue:");
        while (!kbhit()) /* do nothing */ ;
    
        //getchar() (below) is standard C. getch() is part of the 
        //non-standard (but useful), conio.h
        key1 = getch();      //get a letter, w/o an enter key being needed
    
        //print the int that was pressed, as a letter
        printf("\r\nA key was pressed: %c", key1);
    
        //print the same int as it's ascii int value
        printf("\r\nA key was pressed: %d", key1);
    
        printf("\n\t\t\t     Press Enter When Ready ");
        key1 = getchar();   //pause the screen from closing
        
        return 0;
     }

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    103
    OS: Windows/Linux (whichever's easier)

    Compiler: GCC

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Poincare View Post
    OS: Windows/Linux (whichever's easier)

    Compiler: GCC
    It really depends heavily on what you want to do.

    In linux:

    If you just want to have a program that deals with keyboard input in more dynamic way, there's ncurses and/or readline. I think that will solve most real "goal oriented" issues.

    If you have something more technical in mind, well, the keyboard is controlled by the kernel via a module, which is open source and written in C. You could hack that or create a customized one. If you are interested in tooling around with/programming the hardware on a low level, kernel space is where you want to work methinks. The learning curve for the kernel API is a Pretty Challenging at first, and most normal C libs (eg, stdio.h) are not available.
    Last edited by MK27; 06-15-2009 at 09:45 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

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    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.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    103
    Thanks a lot! Seems like what I want to learn about is hidden in the kernel, which, sadly, I'm not elite enough to understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM