Click here to Skip to main content
15,913,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
getkey()
{
union REGS i,o;
i.h.ah=0;
int86(22,&i,&o);
return o.h.ah;
}
main()
{
int k=getkey();
}
Posted

It calls BIOS interrupt number 22 to fetch keystrokes. To be honest, I wouldn't use it - use getch instead.
Hardware access via in86 is severely restricted in XP and higher, or in Linux in any compatible way.
 
Share this answer
 
Comments
Sandeep Mewara 10-Oct-10 12:16pm    
Comment from OP:
Answer 2
Accept Answer Reject Answer

thanks for your help.but now next question arises is how to use getch instead of this code in my program..this code works fine on XP and gives output as k=28.
waiting for your reply....
thanks in advance....
As Griff said, it uses the BIOS to read a keystroke.

Second, the most important point is that it doesn't work on XP. Oh I know, you've compiled your code and it gives an answer but what you're actually doing is compiling it for DOS and running the program under NTVDM (which is the real mode DOS emulator that's been shipped with Windows since NT 3.1 came out yonks ago).

So... (a) bin the antique compiler you're using. 64 bit versions of windows can't run DOS applications so sooner or later "this code works fine" will become "unsupported 16 bit application."

(b) when you've got a decent compiler (VC++ or gcc are both good) then look up how you do I/O. Don't grope around with code from 1991 - find out how modern C and C++ programs work and see how they do it portably.

Cheers,

Ash
 
Share this answer
 
v2
If you just want get the input character.

This code snippet would be sufficient:
C
main()
{
    int k=getchar();
    printf("%d\n",k);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900