Click to See Complete Forum and Search --> : Pressing a key and calling a function


Nauj
06-12-2000, 01:04 PM
I want to know if it's possible to intercept a key in a C program.For example,
to press 'esc' key and call a function. The key could
be pressed in any moment and the function will be called.
If anybody can give an example or give any advice will be greatly
apreciatted.
I'm using Borland TurboC++3.0 (MS-DOS) compiler


Nauj

John Senford
06-14-2000, 04:03 AM
"Nauj" <juanvidalgil@yahoo.com> wrote:
>
>I want to know if it's possible to intercept a key in a C program.For example,
>to press 'esc' key and call a function. The key could
>be pressed in any moment and the function will be called.
>If anybody can give an example or give any advice will be greatly
>apreciatted.
>I'm using Borland TurboC++3.0 (MS-DOS) compiler
>
>
>Nauj


I would use Native functions.
I don't think the Java API provides functions for capturing keyboard input
(other than that sent to the java application).

You will need to write a dll in C in which you have a funciton that monitors
the keyboard input messages (system wide keyboard hooks might be helpful)
[If you don't have it, get MS Visual Dev Studio]

From your Java app you then call this native function to get it started (make
it sit waiting for input) and every time it detects any input it could call
your java function (it can get quite tricky with all this java calling C,
and C calling java)

If no paramters are going to be passed to these functions (i.e. the java
app just needs to run the C function and not actually give it any info, and
visa versa) then it will be much easier.

The Java tuturial might be useful if you need to learn about JNI (Java Native
Interface)

if you want to speak with me further then email me (address above)

good luck,
John