Reading a single keypress

Hey...
I've been trying to workout a way to read a single keypress by the user and put the char in a variable (like the readkey function in pascal).

I tried getc(stdin). But it keeps reading till user pressed enter =/
If anyone managed to do it, please share it with us =)

Thx in advance!
LFLFM

Comments

  • : Hey...
    : I've been trying to workout a way to read a single keypress by the user and put the char in a variable (like the readkey function in pascal).
    :
    : I tried getc(stdin). But it keeps reading till user pressed enter =/
    : If anyone managed to do it, please share it with us =)
    :
    : Thx in advance!
    : LFLFM
    :
    Like INKEY$ in BASIC? There is no standard function in C to do it. I suggest using a bit of inline assembly if your compiler allows it.


  • : : Hey...
    : : I've been trying to workout a way to read a single keypress by the user and put the char in a variable (like the readkey function in pascal).
    : :
    : : I tried getc(stdin). But it keeps reading till user pressed enter =/
    : : If anyone managed to do it, please share it with us =)
    : :
    : : Thx in advance!
    : : LFLFM
    : :
    : Like INKEY$ in BASIC? There is no standard function in C to do it. I suggest using a bit of inline assembly if your compiler allows it.
    :
    :
    :
    Good, you didn't just answer getch() ignoring the fact that it is not standard. However, if your compiler has it (it may be in conio.h or stdio.h or maybe somewhere else) then getch() is a pretty good solution. Better than using some inline assembly (and I'm an assembly freak saying this). If you want to do it right (portability-wise), then a platform independent library would be best, of course that would probably be overkill.

    "We can't do nothing and think someone else will make it right."
    -Kyoto Now, Bad Religion

  • a simple asm routine will do the job:
    [code]
    asm mov ah,0bh
    asm int 21h
    asm xor ah,ah
    INKEY = (AX); /* AX = 0 if no key hit */
    [/code]

    If you compiler won't allow inline assembly, get a modern compiler.

  • [b][red]This message was edited by the Darius at 2002-5-4 15:49:7[/red][/b][hr]
    : a simple asm routine will do the job:
    : [code]
    : asm mov ah,0bh
    : asm int 21h
    : asm xor ah,ah
    : INKEY = (AX); /* AX = 0 if no key hit */
    : [/code]
    :
    : If you compiler won't allow inline assembly, get a modern compiler.
    :
    :

    See, this is what I'm talking about. getch() will work more places, is easier to read, and is more portable and it doesn't require you to know assembly.

    The code above assumes that you are coding for an x86 processor and the DOS operating system.

    Using getch() assumes you are using a compiler that supports it, I believe both Borland and MSVC++ support it (I know Borland does).

    So in retort, a simple non-standard routine will do the job:
    [code]
    char c=getch();
    [/code]

    If your compiler doesn't support getch() then I'd recommend checking your documentation as there is likely to be a function that does the same thing, if you still can't find anything, then I'd recommend using a platform-independent library to handle it or at least a library. (see Curses or S-Lang or others)

    "We can't do nothing and think someone else will make it right."
    -Kyoto Now, Bad Religion



  • : [b][red]This message was edited by the Darius at 2002-5-4 15:49:7[/red][/b][hr]
    : : a simple asm routine will do the job:
    : : [code]
    : : asm mov ah,0bh
    : : asm int 21h
    : : asm xor ah,ah
    : : INKEY = (AX); /* AX = 0 if no key hit */
    : : [/code]
    : :
    : : If you compiler won't allow inline assembly, get a modern compiler.
    : :
    : :
    :
    : See, this is what I'm talking about. getch() will work more places, is easier to read, and is more portable and it doesn't require you to know assembly.
    :
    : The code above assumes that you are coding for an x86 processor and the DOS operating system.
    :
    : Using getch() assumes you are using a compiler that supports it, I believe both Borland and MSVC++ support it (I know Borland does).
    :
    : So in retort, a simple non-standard routine will do the job:
    : [code]
    : char c=getch();
    : [/code]
    :
    : If your compiler doesn't support getch() then I'd recommend checking your documentation as there is likely to be a function that does the same thing, if you still can't find anything, then I'd recommend using a platform-independent library to handle it or at least a library. (see Curses or S-Lang or others)
    :
    : "We can't do nothing and think someone else will make it right."
    : -Kyoto Now, Bad Religion
    :
    :
    :
    :


    The only way to learn is to go ONE STEP beyond what you know now.
    We have two platforms here, INTEL or MOTO. INTEL will prevail.

    : "We have to tell all and think someone else will learn."
    : Truth is better than Bad Religion

  • : : : a simple asm routine will do the job:
    : : : [code]
    : : : asm mov ah,0bh
    : : : asm int 21h
    : : : asm xor ah,ah
    : : : INKEY = (AX); /* AX = 0 if no key hit */
    : : : [/code]
    : : :
    : : : If you compiler won't allow inline assembly, get a modern compiler.
    : : :
    : : :
    : :
    : : See, this is what I'm talking about. getch() will work more places, is easier to read, and is more portable and it doesn't require you to know assembly.
    : :
    : : The code above assumes that you are coding for an x86 processor and the DOS operating system.
    : :
    : : Using getch() assumes you are using a compiler that supports it, I believe both Borland and MSVC++ support it (I know Borland does).
    : :
    : : So in retort, a simple non-standard routine will do the job:
    : : [code]
    : : char c=getch();
    : : [/code]
    : :
    : : If your compiler doesn't support getch() then I'd recommend checking your documentation as there is likely to be a function that does the same thing, if you still can't find anything, then I'd recommend using a platform-independent library to handle it or at least a library. (see Curses or S-Lang or others)
    : :
    : : "We can't do nothing and think someone else will make it right."
    : : -Kyoto Now, Bad Religion
    : :

    : The only way to learn is to go ONE STEP beyond what you know now.
    : We have two platforms here, INTEL or MOTO. INTEL will prevail.
    :
    : : "We have to tell all and think someone else will learn."
    : : Truth is better than Bad Religion
    :
    :

    So everyone should be using and learning INTEL assembly AND THE DOS OPERATING SYSTEM, etlusk? And maybe INTEL will prevail, BUT IT HASN'T YET. There is no Intel monopoly as yet. What if this guy was using a Mac or SGI or was in Windows or Linux or Solaris or OS/2 (an INTEL product). Then I don't know why you didn't use BIOS or direct hardware code. At least they aren't tied to a specific and outdated operating system, and telling him to use function 0xB of DOS, is no better than telling him to use getch(), it's worse. You haven't taught him anything except a single function of an outdated operating system. Why not teach him...

    in al, 60h

    That's not tied to an OS, and that will work in Win95/98 (only because M$ is lazy, it should've been unallowed). It and BIOS will also work before DOS is loaded or in another OS. Maybe he wants to make his own OS someday (PROBABLY NOT) but if he did he could use BIOS or port 60h, he couldn't use DOS Function 0xB.

    Finally, my signature wasn't targetted to this post, it's my SIGNATURE. If you notice, it's in all of my recent (past few months) posts, you can check it out on the C/C++ Board, this board, the x86 Assembly Board, and a few others that I don't routinely check. Your comment about going one step beyond is fine, but learning AN assembly implementation isn't necessarily or typically one step beyond. He went one step beyond when I told him about getch() something I'm assuming he didn't know about in the first place. If I told someone who knew how to implement a list in C, how to implement it in assembly would that be one step beyond in their knowledge of data structures? No. If I told them instead how to implement a binary search tree in C, it would. When replying you have to take in account the target audience (ie the original poster). If you read at the top this is the _BEGINNER_ _C/C++_ board. They probably don't want to handle learning assembly (at least right now). If they did they would probably go to the assembly message board of THEIR PROCESSOR. "Truth is better than Bad Religion." I don't see how that's relevant. My signature wasn't relevant to the post as it's my signature, so responding to it isn't relevant. Anyways, I don't see how that statement or the one above it defend your case in any way, shape or form. You haven't told all, you told an even more specific subset than I did. And the truth is C/C++ is used a LOT more than assembly, portability IS IMPORTANT, Intel is NOT a monopoly yet, and even if it was DOS is DEFINITELY not going to be the OS that everyone is using, there are a whole slew of processors other than Intel's or Motorola's and even ignoring that and limiting to just Intel, not all of Intel's processors are 80x86's.

    "Consensus is not a fact-based exercise"
    - No Substance, Bad Religion
  • I am sorry if in some way I have offended you.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories