Hi,
i would like to know if there r any native functions in C with which i can detect a keypress ( independent of the platform ). The same ofr mouse functions as well.. Any function to detect a mouse click?

If not, how to detect a key press... ?


Thanks.

Recommended Answers

All 35 Replies

>>native functions in C with which i can detect a keypress

getchar() and getc()

>>native functions in C with which i can detect a keypress
no C functions for the mouse.

Hi,
i would like to know if there r any native functions in C with which i can detect a keypress ( independent of the platform ). The same ofr mouse functions as well.. Any function to detect a mouse click?
If not, how to detect a key press... ?
Thanks.

There are as such no standard C/ C++ functions for detecting mouse click and key hits. Its purely compiler or implementation dependent.

But still if you need it either go for a compiler which supports such functionality or an API which provides an easy interface for the above mentioned.

>>native functions in C with which i can detect a keypress

getchar() and getc()
.

thanks... but i wanted something whcih can detect a keypress... I mean... even if some other process or thread is runnung, should a keypress be detected, the program should attend to tht keypress...

something like detecting an event...

Well, using those functions will allow you to check for a key's state while your application is active. I don't think the keypresses can be seen by your program when it's running in the background.

I think for doing that you'll need to use API calls to Win32. I don't know of a standard function that exists for such a purpose.

For win32 i can use case WM_KEYDOWN: tod etect a keypress during the course of a program...
but wat can i do if i have to do teh same for a platfornm independent application???
Thanks,

For win32 i can use case WM_KEYDOWN: tod etect a keypress during the course of a program...
but wat can i do if i have to do teh same for a platfornm independent application???
Thanks,

You can do that with getch(). Here's documentation on it:
http://ou800doc.caldera.com/en/SDK_charm/_getch.html

Basically you call getch() and store an integer, which contains the key code of the last key pressed. Use a loop, and then you've basically got asynchronous input.

But you have to remember that this is not even compiler-independant: it requires each compiler to implement it, so if your main target is Windows, you're best of using the Win32 API.

ok... i don`t think i was very clear the first time round...
i intend to create a miniature gui.. for tht, i need to monitor any user inputs.. i`m presently sticking to key presses..
My question is... how to detect keypresses in such a context?

Thanks...

For win32 i can use case WM_KEYDOWN: tod etect a keypress during the course of a program...
but wat can i do if i have to do teh same for a platfornm independent application???
Thanks,

There is no platform independent way to do what you want. Each platform may (or may not) have a way to do this. You will have to code the program special for each platform.

hmm... ok ... thanks,.
is there anyway to map a key press or mouse click to a macro or something of the sort???

Thanks..

key press can be detected, very easily in C !
just write a function, whuch gets the ascii & scan codes of the key pressed !
wanna its code ... ?

Same abt mouse click !

key press can be detected, very easily in C !
just write a function, whuch gets the ascii & scan codes of the key pressed !
wanna its code ... ?

hey thanks.. thts just wat i wanted...

ya.. the code wud be nice.. if it`s not much of a problem...

Thanks.

>>ya.. the code wud be nice.. if it`s not much of a problem
google for "ascii chart" and you will get all 255 codes

>>ya.. the code wud be nice.. if it`s not much of a problem
google for "ascii chart" and you will get all 255 codes

then u provide the code Mr.Ancient Dragon !

then u provide the code Mr.Ancient Dragon ! vbrep_register("302561")

are you asking ME to do HIS research for HIM :eek: I don't think so -- his fingers can type in the google command just as easy as mine -- and he will get the information a lot faster too.

>>vbrep_register
what's that?

Bcoz, u feel it is so easy to find it on GOOGLE.
Trust me dude, the code is most useful in any proffessional software (made in C).

commented: Stop being an idiot. -1

Bcoz, u feel it is so easy to find it on GOOGLE.
.

yes this one is EASY to find with google -- its the first link that appears. If it was difficult to find then I would have posted a link to it, but its not so I won't. Aspiring programmers have to learn how to do your own research because that's what will be expected of you when you get on a job. Now is just as good a time as any to learn that.

Trust me dude, the code is most useful in any proffessional software (made in C)

There is no code involved -- its only a table of integers that are assigned to each of the 255 possible key values. Also, we were never asked for code -- just function names, and we all answered that question several times. Writing the code to use those functions is an entirely different matter -- google is next to useless for that.

What you're looking for is a macro language, I would suggest taking a look at the AutoIt scripting language, such things are trivialites with this useful language, it can be found at http://www.hiddensoft.com/autoit3/

You can download and find documentation at that site.

thanks for all the replies guys... but as i asked earlier .. how do i get C to scan for the ascii value?? create a thread or anything of the sort??
or is there some other way to continuosly scan for the ascii value? also.. where will this ascii value corresponding to the keypress be stored???

THanks again...

Here is an example of one way to get keyboard input. If you want your program to continue doing something else then yes, you will want to create a thread for the keyboard input function.

>>also.. where will this ascii value corresponding to the keypress be stored?
wherever you want to store them -- that is up to you. What action do you want your program to take when a key is pressed? I assume you want the program to respond to keypresses.

oh God!
u want to read ASCII values ???
there r lot many functions in C for that.
I thought u want the scan code of the key pressed.
I was talking abt sacn code, 'ANCIENT DRAGON' !
I suppose, the programmer need to write his own code to read 'SCAN' code.

The scan codes not standard -- they are different for every keyboard. But if that is what you want, here it is.

Unless you want to write your own keyboard device driver I see no real use for those codes. What, if anything, can you do with them? I have never had an occasion to use them in the past 20+ years. As far as I know there are no standard C functions to get scan codes. You may have to write a hook into the keyboard device driver so that you can capture the codes. I know there are some win32 api functions that return the scan codes, but that of course is non-c standard. I have no clue how to do it in *nix

ok ...
i`m making or tryign to make a GUI library.. which should be able to monitor key striokes from teh user at any instant of time and process those keystrokes accordingly.. the same applies for mouse clicks.

When i click on anywhere on the screen, how does the program know that i have clicked? Is there any way to detect this without using winAPI or any other standard APIs???

Thanks and really sorry for the miusunderstandigns soo far...

u want it for c/c++/vc++ ?

The scan codes not standard -- they are different for every keyboard. But if that is what you want, here it is.

Unless you want to write your own keyboard device driver I see no real use for those codes. What, if anything, can you do with them? I have never had an occasion to use them in the past 20+ years. As far as I know there are no standard C functions to get scan codes. You may have to write a hook into the keyboard device driver so that you can capture the codes. I know there are some win32 api functions that return the scan codes, but that of course is non-c standard. I have no clue how to do it in *nix

wat do say abt the non-alphanumeric keys ?
Say, if anybody want to detect, F1 or ESC, or any such key, wat according to u can be, or shall be done ?
Bcoz, according to my experince on atleast 10 pcs ( I know its small !), I got same scan code.
Like, ESC key has a scan code of '1'.
etc.
etc.

>>s there any way to detect this without using winAPI or any other standard APIs???

No. The only way to get mouse clicks is via operating-system specific calls. And I beleve we have told you that several times in this thread.

>>ay, if anybody want to detect, F1 or ESC, or any such key, wat according to u can be, or shall be done ?

You are confusing scan codes and ascii key codes. They are not the same, so you need to get your terminolory straight. You can use the ascii codes returned by getc() to do what you want, as already demonstrated in this thread. I even posted a link to c code snippets that showed how to do it.

>>>>You are confusing scan codes and ascii key codes

Ok!
theres lot of confusion.
I hav a code,
check it & reply.

#include"dos.h"
#include"stdio.h"
int scan;
/*To find scan code of various function keys*/
getkey()
{
 union REGS i,o;
 i.h.ah=0;
 int86(22,&i,&o);
 scan=o.h.ah;
}
void main()
{
 clrscr();
 printf("Hit any key (ESC to exit)...\n");
 while(1)
 {
  fflush(stdin);
  if(kbhit())
  {
   getkey();
   printf("The scan code of the key u pressed is:%d\n",scan);
   getch();
   if(scan==1)
    exit(0);
  }
 }
}

The function you posted will return the ascii code, not the scan codes, which are the same codes that are returned by getc(). It should work ok with 16-bit MS-DOS compilers such as Turbo C, but will not work with any modern 32-bit compiler because the platform (protected-mode programs on Intell chips) will not allow the program to call any of the ms-dos interrupts.

And your code is very similar to the code in the link I provided in an earlier post to this thread.


Here is a link to some assembly code you might find useful

And an int 16h TSR that hooks into int 16

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.