How to know which key has been pressed in the keyboard

I would like to know how to know which key is pressed on
the keyboard.be it a single stroke or a combination.i am
writing a text editior and would like the usage of ctrl and
alt keys.The problem i face is i am unable to know when a numlock or a caps is pressed(as i am displaying it on the screen).i tried to get the value from port 0x60 but i find it difficult to apply it when the key remains depressed.
i am doing it in borland C++.

I agree with nietod, the best way we can help is if you provide more information. If you want to try to read it from port 60h. then you probably want to write a interupt handler instead of just looking at the port. then inside the handler, you could have some flags for alt, ctrl, shift (If you write a handler you need to handle capitol letters) etc. I can't come up with the code off the top of my head, but if you would like I can post some code I have already written. I would have to dig it up though, so let me know if you want it.
NOTE: This is not the easiest way to do it. If you are writing it in dos, then you might want to do it this way. But if you are doing it in windows, there are lots of easier ways to do this. For example in VC++, you can request the state of a key. (Up or Down) then use that to see if the ctrl key is down when they press another key. Let us know what operating system you are writting for, and what compiler you are using.

The GetKeyboardState function copies the status of the 256 virtual-keyboard keys to the specified buffer.

Parameter Description

lpbKeyState Points to the 256-byte buffer that will receive the virtual-key codes.

Returns

This function does not return a value.

Comments

An application calls the GetKeyboardState function in response to a keyboard-input message. This function retrieves the state of the keyboard at the time the input message was generated.

If the high-order bit is 1, the key is down; otherwise, it is up. If the low-order bit is 1, the key is toggled. A toggle key, such as the CAPSLOCK key, is toggled if it has been pressed an odd number of times since the system was started. The key is untoggled if the low-order bit is 0.
Example

hi luc,
sorry for not accepting your answering.but i am working in borland turbo c++ under DOS platform and it does not does not support ur answer.Please explain a situation where i keep pressing a key (the key pressed has to be displaed and i suddenly press the numlock.i would like to display if the numlock is on or off on the screeen.

Do you mean that the first key (not numlock) would still be held down when the second key (numlock) is pressed?

If so, you cannot use DOS's keyboard handler (It doesn't support two keys beign down seperately, exxcept for the modifier keys (shift, control, alt). Also it doesn not report the numlock and caps lock keys. So it sounds like you will have to write your own driver.

That means hooking the keyboard interrupt each time a key is pressed or released and then communicating with the keyboard to learn what was pressed/released. You then have to "queue" this information so your application can get it later. You can't supply it directly to your application because the interrupt will occur asynchronously. that is going to be a lot of work and You are going to need at least some assembly language to do it. (And each time you screw up as you debug, you will probably have to reboot your computer. that makes it slow.)

nietod,
You can actually do it with no assembly language. C++ makes it very easy to write your onw handlers. But I agree, it is a lot of work, and you can end up rebooting many times before it works right.

girishr,
I do have some code that you can use to help get you started. I wrote it for a game, so it is not quite right. It only looks to see if seven or eight keys are down, then sets flags. But it shoudn't be to hard to set up a queue that you can read off of.

So let's say you've installed an interrupt handler to handle all keyboard
events and when an interrupt is generated your handler reads the byte from
port 60h. What now?

Well..each key on the keyboard has an associated scan code which is contained
in the lower 7 bits of the byte. The most significant bit (ie bit 7) tells
you what was actually done, 0 = key was just pressed, 1 = key was just
released. If someone had just pressed the ESC key for instance, the port will
show a value of 1 (1 is the ESC key's scan code). If they hold their finger
on the button the keyboard will keep generating interrupt 9's and each
time the port will still show a value of 1. When the person releases the key
a final interrupt will be generated and the port will return 129 (1 + 128,
since the high bit will be set indicating the person has released the key).

Well...it's almost this simple. Some keys are "extended" keys. When an
extended key is pressed an interrupt is generated and the keyboard port
will return a value of 224 (E0h). This means that an extended key was pressed
and it's *extended* scan code will be available during the *next* interrupt.
Note that the left control key has a scan code of 29, while the *right*
control key has an *extended* scan code of 29. The same applies to the alt
keys and the arrow keys (keypad arrows vs the other ones).

It would be nice if all keys were created equal and we could just throw away
the 224 extended bytes and handle all the other bytes normally. Unfortunately
there are two buttons which on my machine at least (and others I have tested)
do some really weird stuff:

PrtScn
ÄÄÄÄÄÄ
Pressing this button will send *2* extended characters to the handler, 42
and 55, so the actual byte sequence will be 224, 42, 224, 55. (Also note
that the left shift key has a regular scan code of 42, so there goes our
idea of just throwing 224's away). Only the extended 55's are sent during
auto-repeat. When the key is released, the two are sent again with the high
bits set (224, 170, 224, and 183). If any of the shift or control keys are
being held down when the PrtScn button is pressed then only the (224, 55) is
sent when the key is pressed and only the (224, 183) is sent when it's
released. If the alt key is being held down (System Request) then the key
behaves like an ordinary key with scan code 84. The practical upshot of all
this is that the handlers you write to handle normal keys and extended keys
will work fine with all the different PrtScn combinations (although a program
would have to check normal key 84 *AND* extended key 55 in order to determine
if the key is currently being pressed).

Pause/Break
ÄÄÄÄÄÄÄÄÄÄÄ
Welcome to hell. If you press this key while either of the the control keys
are being held down, it will behave like extended key 70, at all other times
it will send the following bytes: (225, 29, 69, 225, 157, 197). Holding the
key down does not result in autorepeat. Taking your finger off the key does
not send any extra bytes, they appear to be sent after the "key down" bytes
when you first press the key. Notice that 225 isn't 224, so our normal
extended character handler will not take care of this. My personal theory is
that while a scan code of 224 (E0h) means there is 1 more character
following, a scan code of 225 (E1h) means there are *2* more following. I've
seen a number of keyboard handler libraries and they all seem to overlook
this key. So why not be the first kid on your block to have a keyboard
handler which properly supports the Pause/Break key? CHECK IT OUT!!

When I was writing a simple handler to test the info in this file I did
something REALLY stoopid which I would like to share with the world. I
thought that my program was stuffing the keyboard up because when I exited
the program my editor (Borland Pascal 7.0) would act as though the control
button was being held down (I'm sure some of you have already started
laughing by now). I had to press it after each time I ran the program
just to sort it out. After spending a few hours looking all over the place
for info on what could possibly be wrong I realised what I was doing. I was
pressing CTRL-F9 to compile the program which would also immediately make it
run and I was releasing the control key when my program was running, ie the
regular BIOS handler was not getting the control key's "key up" command and
still thought it was being held down when my program returned control to
it. Moron.....

Introduction
This article is the first in a series of articles about the C/C++ Visual Studio Express debugger. It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints. Lastly, Part 3 focuses on th…

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects.
A brief on problem:
Lets take example problem for simplicity:
- I have a G…

The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…