Alhazred Posted July 30, 2004 Share Posted July 30, 2004 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 More sharing options...
0 azcodemonkey Posted July 30, 2004 Share Posted July 30, 2004 Just for the window or for the system? Link to comment Share on other sites More sharing options...
0 Nathan Posted July 30, 2004 Share Posted July 30, 2004 Unplug it. Link to comment Share on other sites More sharing options...
0 neufuse Veteran Posted July 30, 2004 Veteran Share Posted July 30, 2004 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 More sharing options...
0 bakerster Posted July 30, 2004 Share Posted July 30, 2004 Unplug it. :rolleyes: Link to comment Share on other sites More sharing options...
0 Andareed Posted July 30, 2004 Share Posted July 30, 2004 http://www.codeguru.com/vb/gen/vb_system/article.php/c1661/ It's a VB example but it's easily adapted to C. Link to comment Share on other sites More sharing options...
0 Alhazred Posted July 30, 2004 Author Share Posted July 30, 2004 Yes, it's just for the windowed application. Link to comment Share on other sites More sharing options...
0 azcodemonkey Posted July 30, 2004 Share Posted July 30, 2004 A keyboard hook is your best bet, as Andareed pointed out. I was thinking that hooks were only system-wide. but they can be used for a specific window. http://msdn.microsoft.com/library/default....sdn_hooks32.asp Link to comment Share on other sites More sharing options...
0 Alhazred Posted July 31, 2004 Author Share Posted July 31, 2004 Thanks, I'll take a look there :) Link to comment Share on other sites More sharing options...
0 +John Teacake MVC Posted July 31, 2004 MVC Share Posted July 31, 2004 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 More sharing options...
0 Alhazred Posted July 31, 2004 Author Share Posted July 31, 2004 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 More sharing options...
0 neufuse Veteran Posted July 31, 2004 Veteran Share Posted July 31, 2004 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 More sharing options...
0 Andareed Posted July 31, 2004 Share Posted July 31, 2004 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 More sharing options...
0 Alhazred Posted July 31, 2004 Author Share Posted July 31, 2004 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 More sharing options...
0 Andareed Posted August 1, 2004 Share Posted August 1, 2004 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 More sharing options...
0 Alhazred Posted August 1, 2004 Author Share Posted August 1, 2004 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 More sharing options...
0 bithub Posted August 2, 2004 Share Posted August 2, 2004 Ack, why are you guys suggesting he install hooks for such a simple problem? Just flush the console stdin before you ask for the user input. fflush(stdin); Link to comment Share on other sites More sharing options...
0 Alhazred Posted August 2, 2004 Author Share Posted August 2, 2004 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 More sharing options...
0 bithub Posted August 2, 2004 Share Posted August 2, 2004 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 More sharing options...
0 Alhazred Posted August 5, 2004 Author Share Posted August 5, 2004 I solved with FlushConsoleInputBuffer(hInput) Link to comment Share on other sites More sharing options...
Question
Alhazred
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