reading keystrokes

This is a discussion on reading keystrokes within the C++ Programming forums, part of the General Programming Boards category; Hi I am a newbie when it comes to C++ but I have learned basic. What I want to know ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    37

    Exclamation reading keystrokes

    Hi I am a newbie when it comes to C++ but I have learned basic. What I want to know is how to make it so that right when you press whatever key you press it reacts not when you press enter but that second. In basic it is
    Code:
    print #(whatever you have the thing you use as), "when characterInput [letter]"
    wait
    waht it does is it will wait and then if you press a key it will jump to [letter]. So how can I do this in C++ and another thing that I need wich is tied into this is how to set a varible to the button pressed. In basic it is x$ = inkey$ "in basic the strings have a $ at the end).

    thanks in advanced,
    h3ckf1r3

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It can be done, but you have to use some non-standard functions [As you are probably aware, the Basic programming language is fairly well "unstandard" in itself, so you can't easily take a piece of Basic code written for an Atari 600 and run it on a PC, for example].

    So you will have to tell us what OS (and compiler) you are using, and we should be able to point you in the right direction.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    37
    I am using windows xp and I am using tha Dev-C++ compiler

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And is this a console or Window application?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    37
    I am not sure what you mean

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    There's a FAQ entry to show you how to read a key without pressing enter, when using the win32 API.
    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.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    37
    I will give it a shot

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    37
    it told me to use the command getchar() but how do I use that to set it to a variable?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    char ch;
    
    ch = getChar();
    Perhaps?

    I'm assuming you are using the last of the listed examples.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    37
    yeah I tried that but it said that getchar() or getChar() was undeclared. Maybe I dont have the right includes or the wrong namespace please tell me wich are necessary.

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    In that example getChar is a user-defined function using functions from <windows.h>
    Last edited by anon; 01-15-2008 at 09:14 AM.
    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).

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, getChar() is part of the example-code, so you need to include the 15-20 lines BEFORE main in that example to make it work - did you do that?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    37
    I am probably reading the wrong thing so please be more explicit where the thread on this is

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It's not a thread, but a FAQ entry: http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    It has three bits of sample code: I think you are looking at the last section, which begins with
    And now for a Windows specific solution:
    .

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    37
    waht is up with all that extra stuff at the top?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-05-2009, 02:14 AM
  2. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  3. Replies: 2
    Last Post: 01-28-2008, 02:07 AM
  4. Reading a record from a File
    By David101 in forum C Programming
    Replies: 2
    Last Post: 12-14-2004, 05:42 PM
  5. Replies: 0
    Last Post: 07-12-2002, 01:40 PM

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