Teen Programmers Unite  
 

 

Return to forum top

disable keyboard keys in c++

Posted by Cybermonk [send private reply] at August 29, 2002, 11:06:53 AM

im writing a custom password program for my system and its console. runs at startup. wont fool anyone good from getting through but some naughty people around.
But Ctrl-c stops execution. can i someway disable the ctrl key and any other one???
the cybermonk

Posted by Psion [send private reply] at August 29, 2002, 11:32:12 AM

Look up the signal function (and the associated constant SIGINT) in the standard C library.

Posted by whizkide [send private reply] at August 30, 2002, 12:49:03 PM

ok thanx psion.
the signal function prototype
void (*signal(int sig, void(*func)(int)))(int);
does the trick.
and the signal is SIGINT and returns SIG_ERR if theres an error.
well thats for anyone else having the same problem.
whizzzzzzzzzz

Posted by whizkide [send private reply] at August 30, 2002, 02:40:38 PM

ok i tried that. check this test program out
#include<csignal>
#include<iostream>
using namespace std;
void handler(int);
int main()
{signal(SIGINT,handler);
while(true)
{char ch;
cin>>ch;
if(ch=='q')break;
}
return 0;
}
void handler(int n)
{//bla bla bla
}
it only calls handler the first Ctrl-c is pressed. but if i called the signal function within the loop it holds throughout. why???????

Posted by unknown_lamer [send private reply] at August 30, 2002, 05:20:24 PM

It looks like Windows calls a handler and then uninstalls the handler. Some implementations do this and IIRC it is completely conforming to ANSI C. The signal man page says this:

The original Unix signal() would reset the handler to
SIG_DFL, and System V (and the Linux kernel and libc4,5)
does the same. On the other hand, BSD does not reset the
handler, but blocks new instances of this signal from
occurring during a call of the handler. The glibc2
library follows the BSD behaviour.

So you should reinstall the signal handler from within the signal handler when you use it, or use something like sigaction is Windows has that.

Posted by Neumann [send private reply] at August 30, 2002, 08:31:08 PM

MSDN points out that there is a problem with using SIGINT under Windows. It's says that it's "Not Supported". So IMHO, it's may not be related to what U_L said.

Check the C runtime reference on MSDN for more infos (I sent you the link yesterday)

Posted by whizkide [send private reply] at August 31, 2002, 12:36:38 PM

aight UL and neumann. all i had to do was re-register the signal handler from within the handler.
thanx.
Also, when using getch() in a loop and using a condition like
{if((c=getch())=='\n')
why do i have to press ctrl-enter for it to send the newline character???:-)
thanx yall.
whizzzzzzzzzzzzzzzzzz

Posted by whizkide [send private reply] at September 02, 2002, 05:24:32 PM

dont bother yall. all i had to do was replaced that ugly "\n" with the ASCII code for return "\r" thats 13.
thanx anywayz
whizzzzzzzzzzzzzzzzzz
ps it worked like magic!

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.