• 0

[C] Keybord


Question

I'm writing a program for Win NT and at a certain point I need to disable the keybord.

Does exist a function/API that disable the keybord and enable it when I want?

Link to comment
Share on other sites

19 answers to this question

Recommended Posts

  • 0
I'm writing a program for Win NT and at a certain point I need to disable the keybord.

Does exist a function/API that disable the keybord and enable it when I want?

why in the world would you want to disable the keyboard? i sure hope you mean for a windowed app and not the entire system

Link to comment
Share on other sites

  • 0
Unplug it.

Yeah that usually works in C :p :rolleyes: :laugh: . I dont know if this is good programming practice but couldnt you just create a certain procedure/function that has a loop with a getch() and then just discard the character every time.

Link to comment
Share on other sites

  • 0

Now I'll try to explain what I need to do.

I have two processes (opened in a console window eachone) which cooperate, one of these processes is in waiting status, the other accept an input from a user, then send the input to the first process which elaborate it. After these operations the processes exchange the roles between them, the first waits the datas from the second, which accept an input from a user and so on.

Here's the problem, if I write something in the console of the waiting process, it accepts that as input when I should insert the real input.

I need to know this, how can I do to discard the input inserted by a user to a process when it's waiting for an input from the other process and not from the user?

Sorry for my bad explanation, but I'm not so good with english :p

Link to comment
Share on other sites

  • 0
Yeah that usually works in C :p :rolleyes: :laugh: . I dont know if this is good programming practice but couldnt you just create a certain procedure/function that has a loop with a getch() and then just discard the character every time.

someone obviously doesnt care about CPU usage ;)

Link to comment
Share on other sites

  • 0

getch and most similar functions block the input thread so cpu usage would be minimal.

You could probably also use SuspendThread. You would have to somehow tell the other process what the thread id is.

Link to comment
Share on other sites

  • 0

There's no problem to give the ID to the other process, I already do that for another thing, explain me what should I do with the ID to solve my problem.

Link to comment
Share on other sites

  • 0

HANDLE hThread = OpenThread(THREAD_SUSPEND_RESUME, FALSE, dwThreadId);

SuspendThread(hThread);

CloseHandle(hThread);

It is probably better to use windows hooks though:

SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, NULL, dwThreadId);

LRESULT CALLBACK LowLevelKeyboardProc(int nCode,

WPARAM wParam,

LPARAM lParam

);

You shoudl check MSDN for more details.

Link to comment
Share on other sites

  • 0

I thought this thing, I could use SetActiveWindow to focus the right console window, but how I have to do to take a console handle?

Link to comment
Share on other sites

  • 0

I tried to do that, but it doesn't works.

P.S. Isolved the problem about focus the console, but I still need to empty the keyboard buffer.

Link to comment
Share on other sites

  • 0

Well I did some reading on fflush(), and it turns out that it doesn't work on all operating systems. The function is mainly used to flush output file streams, and the use of it flushing input streams is undefined.

Anyways, from what I've read, there is no way to flush an input stream and discard the data entirely. One work around is just to do an extra fgets with a large buffer size to read in everything the user might have typed in.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.