0

i was wondering if there is a way in C to detect when user is typing. I actually read this post C detect user typing characters in stdin but i can't find a way to switch my console from line-mode to char-mode in Windows OS...

Is "ncurses" library as it said, the only way to do that?... Can't i just create a thread that observe the "input" stream like in java?

I need only the "event handler", the rest of my code will send a message to a client through Winsock like "USER_NAME is typing" like Whatsapp...

is there any way to do that?

2 Answers2

1

If using Windows, then you can use GetAsyncKeyState(). It is defined and implemented in Windows.h and implemented using User32.lib and User32.dll. It not only detects key clicks, but also provides information about state of key and order of key clicks:

Determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState.

It is also capable of handling sequences of key strokes, including those of virtual keys as shown below:

A short example:

short state=0;
short state1=0;

state = GetAsyncKeyState(VK_CONTROL);
if (0x80000000 & state) //check instantaineous state of key
{
    state = GetAsyncKeyState(VK_SHIFT); 
    if (0x80000000 & state) //check instantaneous state of key
    {
        state = GetAsyncKeyState('h'); 
        state1 = GetAsyncKeyState('H'); 
        if ((0x80000000 & state) || 
            (0x80000000 & state1))

This particular example is an excerpt for trapping sequences of modified key clicks, but the function can be used to simply trap any key that is pressed.

ryyker
  • 22,849
  • 3
  • 43
  • 87
  • Okay, so with this code i'm going to know if the user press a key and then send my message right? I suppose that i'll need a thread for this right? – Simone De Luca Dec 18 '20 at 17:43
  • @SimoneDeLuca - In my implementation, I did not need to use a separate thread For my purposes I just ran it to allow user to set states. But yes, it can also be used in a separate thread.\ – ryyker Dec 18 '20 at 17:46
0

Sounds like "kbhit" might work for you. There is an example for it.

  • This is a good solution. The answer though would be much improved if a small sample code was included. This prevents answers from becoming useless when/if the link goes stale. – ryyker Dec 18 '20 at 17:54
  • Inan thank you very much, i think i'll pick your code and not @ryyker 's one because i need to know if ANY KEY on the keyboard is pressed, not a SPECIFIC KEY like GetAsyncKeyState method wants. I'll put it in a thread launched before the scanf instruction. Please tell me if it could be a good solution for your opinion... – Simone De Luca Dec 18 '20 at 17:57
  • I personally don't use khbit function very often, just saw it a while back. Your following solution might be little tricky but should work. Glad it helped. And you're right @ryyker, this time I didn't want to copy and paste other existing solution but next time I'll improve my solutions, thanks. – İnan Uygur Dec 19 '20 at 10:24
  • In case you wonder, I am not the down-vote. In this case, because `kbhit()` is easily searchable, it is not worthy of a down-vote IMO. But the suggestion to include an example as opposed to a link was offered because it is seen as important by most people that frequent this site. As explained - links disappear. btw, it is not wrong to cut and paste an example from another site as long as that site is given credit. This answer will be visited time and time again. If you improve it a little, it will have higher potential to get voted up. – ryyker Dec 19 '20 at 15:39