Disable keyboard input with C/C++
Jan 19, 2005 at 4:27 AM Thread Starter Post #1 of 16

Stephonovich

Headphoneus Supremus
Joined
Jan 6, 2003
Posts
3,260
Likes
11
I'm trying to figure out how to disable keyboard input to a program (ideally not OS specific, but the main target for this is Windows). I Googled and looked at Experts-Exchange, but all the solutions I found required an external .dll or .exe. I want something that's embedded in the program itself. There should be some sort of key combination that will close it, as well. Don't want them to have to do a hard reset just to kill it, after all.

And no, this isn't something malicious. It's for a friend's senior prank. I wrote the program a few years ago, then did nothing with it. If you wish to view the source, it's here. Or, for the compiled program, here. Again, I must stress, this is NOT an actual virus, it merely mimics it.

Also of note, this is still a work in progress. Once I get the keyboard issue figured out, I'm going to change some text a bit, add a couple more things, blah blah...
 
Jan 19, 2005 at 4:51 AM Post #2 of 16
Your program already has keyboard input disabled, for the most part. Just don't handle keyboard input or messages, and the keyboard is effectively disabled. If what you really want to do is disable Ctrl+C or Ctrl+Break, use SetConsoleCtrlHandler().
 
Jan 19, 2005 at 6:44 AM Post #3 of 16
Nah; just changed the code around some to give more of an illusion that the system is indeed hosed. Security through obscurity
biggrin.gif


Thanks for the suggestion, though.

I'm quickly finding out that trying to do OS level stuff is much, much harder than I expected. End up having to include massive libraries and more code than the entire program. Yeesh.
 
Jan 19, 2005 at 8:27 AM Post #4 of 16
Quote:

Originally Posted by Stephonovich
I'm quickly finding out that trying to do OS level stuff is much, much harder than I expected. End up having to include massive libraries and more code than the entire program. Yeesh.


Well, that is the nature of programming in general. But in this case,
#include <windows.h>
and
SetConsoleCtrlHandler(NULL, TRUE);
is all you need to kill CTRL+C. Pretty simple.
 
Jan 19, 2005 at 9:12 AM Post #5 of 16
Quote:

Originally Posted by Wodgy
Well, that is the nature of programming in general. But in this case,
#include <windows.h>
and
SetConsoleCtrlHandler(NULL, TRUE);
is all you need to kill CTRL+C. Pretty simple.



Alright; decided to try it. I get this:

Code:

Code:
[left]$ g++ hdwipe.cpp -o hdwipe.exe hdwipe.cpp:49: ISO C++ forbids declaration of `SetConsoleCtrlHandler' with no type hdwipe.cpp:49: `int SetConsoleCtrlHandler' redeclared as different kind of symbol c:/mingw/include/wincon.h:160: previous declaration of `BOOL SetConsoleCtrlHandler(BOOL (*)(long unsigned int), int)' hdwipe.cpp:49: initializer list being treated as compound expression[/left]

Checked MSDN; yep, I've got all the required libraries and header files. Using gcc-3.2.3 (mingw) with msys-1.0.

Here's the code again...

I'm doing something horribly wrong and easy, I'm guessing...
 
Jan 19, 2005 at 9:24 AM Post #6 of 16
You need to move the function call inside main(). Right now it's sitting in the outermost scope, which means it's acting as a declaration, not a function call (and the declaration has an invalid syntax). The function is already declared in windows.h, so there is no need to declare it again.

Also, you'll need to change your command line when compiling to link with Kernel32.lib.
 
Jan 19, 2005 at 9:36 AM Post #7 of 16
Moving it inside main() results in gcc spitting out syntax errors about pretty much every line in the program. Also; I looked, and I didn't have a kernel32.lib. Had libkernel32.a, which I assumed to be the same. However, did an allinurl:kernel32.lib Google, found a file, and tried that. Still no go.

I've done no Windows programming up until this time, if you haven't noticed. Most advanced stuff I've ever gotten into was a 600 line program that calculated who'd win a footrace depending on a ton of variables, with some pseudorandom events.
 
Jan 19, 2005 at 9:59 AM Post #8 of 16
Quote:

Originally Posted by Stephonovich
Moving it inside main() results in gcc spitting out syntax errors about pretty much every line in the program. Also; I looked, and I didn't have a kernel32.lib. Had libkernel32.a, which I assumed to be the same.


Well, there are two separate issues here. If you leave your command line the same as before, compilation should work, but the linker should give you an error. What actual syntax errors are you getting?

"libkernel32.a" is some kind of Cygwin/GNUism. If you're going to be writing programs for Windows, you should download the actual Win32 SDK from Microsoft, where you will find kernel32.lib and all the rest of the actual Windows import libraries.

Quote:

I've done no Windows programming up until this time, if you haven't noticed. Most advanced stuff I've ever gotten into was a 600 line program that calculated who'd win a footrace depending on a ton of variables, with some pseudorandom events.


Everyone needs to get started somewhere
smily_headphones1.gif
Anyway, if you're serious about learning Windows programming, it is worthwhile to get a book on the Win32 API.
 
Jan 19, 2005 at 10:10 AM Post #9 of 16
What errors...

Code:

Code:
[left]$ g++ hdwipe.cpp /mingw/lib/kernel32.lib -o hdwipe.exe hdwipe.cpp: In function `int main()': hdwipe.cpp:52: parse error before `(' token hdwipe.cpp:52: ISO C++ forbids declaration of `SetConsoleCtrlHandler' with no type hdwipe.cpp:52: `int SetConsoleCtrlHandler' redeclared as different kind of symbol c:/mingw/include/wincon.h:160: previous declaration of `BOOL SetConsoleCtrlHandler(BOOL (*)(long unsigned int), int)' hdwipe.cpp:52: initializer list being treated as compound expression hdwipe.cpp:53: parse error before `{' token hdwipe.cpp:55: syntax error before `<<' token hdwipe.cpp:56: ISO C++ forbids declaration of `pause' with no type hdwipe.cpp:56: `int pause' redeclared as different kind of symbol hdwipe.cpp:31: previous declaration of `void pause(long int)' hdwipe.cpp:57: ISO C++ forbids declaration of `system' with no type hdwipe.cpp:57: `int system' redeclared as different kind of symbol c:/mingw/include/stdlib.h:375: previous declaration of `int system(const char*) ' hdwipe.cpp:57: invalid conversion from `const char*' to `int' hdwipe.cpp:58: syntax error before `<<' token hdwipe.cpp:59: syntax error before `<<' token hdwipe.cpp:60: ISO C++ forbids declaration of `dots' with no type hdwipe.cpp:60: `int dots' redeclared as different kind of symbol hdwipe.cpp:38: previous declaration of `void dots(long int, int)' hdwipe.cpp:60: initializer list being treated as compound expression hdwipe.cpp:61: syntax error before `<<' token hdwipe.cpp:62: ISO C++ forbids declaration of `dots' with no type hdwipe.cpp:62: redefinition of `int dots' hdwipe.cpp:60: `int dots' previously defined here hdwipe.cpp:62: initializer list being treated as compound expression hdwipe.cpp:63: syntax error before `<<' token hdwipe.cpp:64: ISO C++ forbids declaration of `dots' with no type hdwipe.cpp:64: redefinition of `int dots' hdwipe.cpp:62: `int dots' previously defined here hdwipe.cpp:64: initializer list being treated as compound expression hdwipe.cpp:65: ISO C++ forbids declaration of `pause' with no type hdwipe.cpp:65: redefinition of `int pause' hdwipe.cpp:56: `int pause' previously defined here hdwipe.cpp:66: syntax error before `<<' token hdwipe.cpp:67: ISO C++ forbids declaration of `pause' with no type hdwipe.cpp:67: redefinition of `int pause' hdwipe.cpp:65: `int pause' previously defined here hdwipe.cpp:68: ISO C++ forbids declaration of `system' with no type hdwipe.cpp:68: redefinition of `int system' hdwipe.cpp:57: `int system' previously defined here hdwipe.cpp:68: invalid conversion from `const char*' to `int' hdwipe.cpp:69: syntax error before `<<' token hdwipe.cpp:70: syntax error before `<<' token hdwipe.cpp:71: syntax error before `<<' token hdwipe.cpp:72: ISO C++ forbids declaration of `dots' with no type hdwipe.cpp:72: redefinition of `int dots' hdwipe.cpp:64: `int dots' previously defined here hdwipe.cpp:72: initializer list being treated as compound expression hdwipe.cpp:73: syntax error before `<<' token hdwipe.cpp:74: syntax error before `<<' token hdwipe.cpp:75: ISO C++ forbids declaration of `dots' with no type hdwipe.cpp:75: redefinition of `int dots' hdwipe.cpp:72: `int dots' previously defined here hdwipe.cpp:75: initializer list being treated as compound expression hdwipe.cpp:76: syntax error before `<<' token hdwipe.cpp:77: ISO C++ forbids declaration of `dots' with no type hdwipe.cpp:77: redefinition of `int dots' hdwipe.cpp:75: `int dots' previously defined here hdwipe.cpp:77: initializer list being treated as compound expression hdwipe.cpp:78: syntax error before `<<' token hdwipe.cpp:79: syntax error before `<<' token hdwipe.cpp:80: ISO C++ forbids declaration of `dots' with no type hdwipe.cpp:80: redefinition of `int dots' hdwipe.cpp:77: `int dots' previously defined here hdwipe.cpp:80: initializer list being treated as compound expression hdwipe.cpp:81: syntax error before `<<' token hdwipe.cpp:82: syntax error before `<<' token hdwipe.cpp:83: ISO C++ forbids declaration of `dots' with no type hdwipe.cpp:83: redefinition of `int dots' hdwipe.cpp:80: `int dots' previously defined here hdwipe.cpp:83: initializer list being treated as compound expression hdwipe.cpp:84: syntax error before `<<' token hdwipe.cpp:85: syntax error before `<<' token hdwipe.cpp:86: ISO C++ forbids declaration of `dots' with no type hdwipe.cpp:86: redefinition of `int dots' hdwipe.cpp:83: `int dots' previously defined here hdwipe.cpp:86: initializer list being treated as compound expression hdwipe.cpp:87: syntax error before `<<' token hdwipe.cpp:88: syntax error before `<<' token hdwipe.cpp:89: ISO C++ forbids declaration of `dots' with no type hdwipe.cpp:89: redefinition of `int dots' hdwipe.cpp:86: `int dots' previously defined here hdwipe.cpp:89: initializer list being treated as compound expression hdwipe.cpp:90: syntax error before `<<' token hdwipe.cpp:91: syntax error before `<<' token hdwipe.cpp:92: ISO C++ forbids declaration of `dots' with no type hdwipe.cpp:92: redefinition of `int dots' hdwipe.cpp:89: `int dots' previously defined here hdwipe.cpp:92: initializer list being treated as compound expression hdwipe.cpp:93: syntax error before `<<' token hdwipe.cpp:94: syntax error before `<<' token hdwipe.cpp:95: ISO C++ forbids declaration of `dots' with no type hdwipe.cpp:95: redefinition of `int dots' hdwipe.cpp:92: `int dots' previously defined here hdwipe.cpp:95: initializer list being treated as compound expression hdwipe.cpp:96: syntax error before `<<' token hdwipe.cpp:97: syntax error before `<<' token hdwipe.cpp:98: ISO C++ forbids declaration of `dots' with no type hdwipe.cpp:98: redefinition of `int dots' hdwipe.cpp:95: `int dots' previously defined here hdwipe.cpp:98: initializer list being treated as compound expression hdwipe.cpp:99: syntax error before `<<' token hdwipe.cpp:100: syntax error before `<<' token hdwipe.cpp:101: ISO C++ forbids declaration of `dots' with no type hdwipe.cpp:101: redefinition of `int dots' hdwipe.cpp:98: `int dots' previously defined here hdwipe.cpp:101: initializer list being treated as compound expression hdwipe.cpp:102: syntax error before `<<' token hdwipe.cpp:103: syntax error before `<<' token hdwipe.cpp:104: ISO C++ forbids declaration of `dots' with no type hdwipe.cpp:104: redefinition of `int dots' hdwipe.cpp:101: `int dots' previously defined here hdwipe.cpp:104: initializer list being treated as compound expression hdwipe.cpp:105: syntax error before `<<' token hdwipe.cpp:106: syntax error before `<<' token hdwipe.cpp:107: ISO C++ forbids declaration of `dots' with no type hdwipe.cpp:107: redefinition of `int dots' hdwipe.cpp:104: `int dots' previously defined here hdwipe.cpp:107: initializer list being treated as compound expression hdwipe.cpp:108: syntax error before `<<' token hdwipe.cpp:109: syntax error before `<<' token hdwipe.cpp:110: ISO C++ forbids declaration of `dots' with no type hdwipe.cpp:110: redefinition of `int dots' hdwipe.cpp:107: `int dots' previously defined here hdwipe.cpp:110: initializer list being treated as compound expression hdwipe.cpp:111: syntax error before `<<' token hdwipe.cpp:112: syntax error before `<<' token hdwipe.cpp:113: syntax error before `<<' token hdwipe.cpp:114: syntax error before `<<' token hdwipe.cpp:115: syntax error before `<<' token hdwipe.cpp:116: syntax error before `<<' token hdwipe.cpp:117: syntax error before `<<' token hdwipe.cpp:118: syntax error before `<<' token hdwipe.cpp:119: syntax error before `<<' token hdwipe.cpp:122: syntax error before `>>' token[/left]

Everything, as you can see. And, oddly enough, it still yells about SetConsoleCtrlHandler up at the top.

Thanks for all the help; BTW. I really have to get to bed if I want to be conscious tomorrow. I'll check back from AB Tech. I'll have a compiler there, so I can play with it, too.

Oh yeah... where is the SDK available for download? Searched MSDN and Microsoft's sites, but couldn't find anything. Google just brought up information about it.
 
Jan 19, 2005 at 10:22 AM Post #10 of 16
Sounds like something is still wrong. Can you upload the most recent version of the program source?

The Win32 SDK is now called the Windows Platform SDK, and you can get it here:
http://www.microsoft.com/msdownload/...uplauncher.asp

BTW, I think it's great that you're learning to program using the basic command line tools as a start, rather than a full-scale IDE. Knowing the basics and what's going on behind the scenes is valuable. However, you probably shouldn't hobble yourself by trying to work with the Cygwin or MinGW toolset on Windows. There is no real advantage to doing this while you're learning, and it will just confuse and frustrate you. It's worth downloading the complete, genuine platform SDK, and perhaps the free MS command-line compiler as well just so that things are easy to set up and you can concentrate on learning. You will also find that the modern MS compiler has better and more readable error messages than the GNU compiler.
 
Jan 19, 2005 at 5:02 PM Post #11 of 16
Updated code here.

When I first started coding I used Borland, but later settled on gcc. I've never had any problem with it that I couldn't fix up until this point, actually. Google's always been able to help me. I am getting the SDK, and will give it a try, but I've just never liked IDEs. Too big, and they always seem to coddle you. That's why I like gcc (and most open source stuff); they don't pull any punches. They tell you exactly what went wrong, and it's up to you to fix it. Guess I'm a 'learn to swim by jumping in' kind of person
biggrin.gif
 
Jan 19, 2005 at 5:19 PM Post #12 of 16
The SetConsoleCtrlHandler() function call is still not in main(). That's why you're getting all those errors.

BTW, the free downloadable Microsoft compiler is a command-line tool. You won't have to surrender your dislike of IDEs in order to use it. MSVC.net (the thing with the IDE) is not free.
 
Jan 19, 2005 at 6:57 PM Post #13 of 16
Quote:

Originally Posted by Wodgy
The SetConsoleCtrlHandler() function call is still not in main(). That's why you're getting all those errors.

BTW, the free downloadable Microsoft compiler is a command-line tool. You won't have to surrender your dislike of IDEs in order to use it. MSVC.net (the thing with the IDE) is not free.



Argh... I feel like an arse. I can't believe I missed that. Live and learn, I suppose...

Anyway, finished downloading the SDK and compiler a bit ago. Currently installing it, so we'll see how it goes.

Again, thanks for all your help. Incompetent newbies everywhere thank you
biggrin.gif
 
Sep 6, 2011 at 8:30 AM Post #14 of 16
I'm sorry to interrupt in between but could you two indulge me in your discussion..i too wanted to disable my keyboard and mouse coz wheneva my friends come to my home they mess around with my pc..
i have got Windows XP. service pack2 installed and i'd like to code the required program in c language..
 

Users who are viewing this thread

Back
Top