How to Get Keyboard event????

This is a discussion on How to Get Keyboard event???? within the C++ Programming forums, part of the General Programming Boards category; Dear Sir, I am doing project..one part of project i wanted to catch the keyboard events like if i press ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    33

    How to Get Keyboard event????

    Dear Sir,

    I am doing project..one part of project i wanted to catch the keyboard events like if i press A ,B...means i should get equivalant value
    Ex: if i assign A="string" means if i press A means it should display string
    overally How to catch the keyboard events?
    give me logic i will try to implement..if you found any useful links for more information please provide me..
    Thanks in advance to all
    Last edited by Salem; 12-18-2007 at 06:27 AM. Reason: No need for code tags

  2. #2
    Registered User
    Join Date
    Aug 2007
    Posts
    33
    How to catch the keyboard events?

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    Does it involve special characters such as backspace/return? Do you need low-level access? Do you plan to program for Windows using dialogs?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User t3chn0n3rd's Avatar
    Join Date
    Dec 2007
    Location
    kansas city
    Posts
    25

    shared libraries for keyboard input

    I am assuming the only library to be included is the standard

    #include <stdio.h>

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If it is a concole application it might be easier to take the usual route: let the user type 'A' and Enter and use standard input methods.

    There is no natural connection between keyboard keys and variable names in the program. It does not matter at all if you call your string variable A or something else. It is up to you, as a programmer, to ensure that the correct string is displayed.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    15
    For getting the character without having to wait for enter:
    http://msdn2.microsoft.com/en-us/library/ms684961.aspx

    if you don't want to study it, then:

    Code:
    #include <windows.h>
    #include <iostream>
    
    using namespace std ;
    
    
    
    
    char GetInput()
    {
    HANDLE input=GetStdHandle(STD_INPUT_HANDLE) ;
    LPDWORD length ;
    INPUT_RECORD record ;
    char ToReturn ;
         while (record.Event.KeyEvent.bKeyDown)
        {
       ReadConsoleInput(input,&record,1,length) ;
       ToReturn= record.Event.KeyEvent.uChar.AsciiChar ;
    
       }
    return ToReturn ;
    }
    
    //now whenever testing the function:
    
    int main()
    {
    
    string Letter_A="Amazing addition to my function collection really works" ;
    if (GetInput()=='a')
    cout<<Letter_A ;
    else cout<<"You either didn't enter \"a, or Garland tried to fool me! " ;
    cin.get();
    return 0 ;
    
    
    
    }
    I have tested it for myself, so I think it should work.
    Last edited by Garland; 12-18-2007 at 09:41 AM.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    Code makes my eyes weep. So poorly indented I don't want to look at it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    15
    I won't be trying to start a flame here, because it is a) stupid and b) I know that the code indeed doesn't look very nice,
    but perhaps it would be better to submit your own solution to the problem instead of just writing that easy sentence.

    I've seen some of your other posts, and it is for people like you, who pretend to know everything, why even watching some forums is so painful.

    Note that I do admit the bad indent of the code, but how about showing some euphemism?

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    Code:
    #include <windows.h>
    #include <iostream>
    
    using namespace std ;
    
    char GetInput()
    {
    	HANDLE input=GetStdHandle(STD_INPUT_HANDLE) ;
    	LPDWORD length ;
    	INPUT_RECORD record ;
    	char ToReturn ;
    	while (record.Event.KeyEvent.bKeyDown)
    	{
    		ReadConsoleInput(input,&record,1,length) ;
    		ToReturn= record.Event.KeyEvent.uChar.AsciiChar ;
    	}
    	return ToReturn ;
    }
    
    //now whenever testing the function:
    int main()
    {
    	string Letter_A="Amazing addition to my function collection really works" ;
    	if (GetInput()=='a')
    		cout<<Letter_A ;
    	else
    		cout<<"You either didn't enter \"a, or Garland tried to fool me! " ;
    	cin.get();
    	return 0 ;
    }
    Indented and looks SO much better.
    Even if you're posting a sample, please INDENT the code, because otherwise it's almost unreadable - like your previous example was.

    It's not too much to ask of you to actually indent your code, is it? That's all I ask.
    (Not criticizing your post nor your code - just your indentation.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Aug 2007
    Posts
    15
    Very well then.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  2. Keyboard port using other that a keyboard
    By antoinelac in forum C++ Programming
    Replies: 4
    Last Post: 06-12-2008, 02:46 PM
  3. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 09:00 AM
  4. keyboard event
    By SuperNewbie in forum Windows Programming
    Replies: 4
    Last Post: 06-10-2002, 06:51 PM

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