Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I'm real new to C programming

Status
Not open for further replies.

doctor2001

IS-IT--Management
Oct 21, 2001
71
CA
Just wondering if someone can help me a little...
I am trying to make a Dos application that executes in the autoexec.bat. The program will run, and if a funtion key is pressed it will run a batch file. If it is not pressed it will self-terminate and comtinue to load Windows. I am a newbie to C programming and this is what I have so far...

# include <stdio.h>
# include <stdlib.h>

main ( )
{

system(&quot;cls&quot;);
system(&quot;run&quot;); run is the batch file
}

Could someone tell me how to use a keypress function and then I could put it into an if statement...I think.


Thanks I advance...I trying o learn on my own !!!
 
Use the getch() function, it stands for getChar. Getting the F6 btn I'm unsure about but it probably entails using the ascii codes to get it. Rocco is the BOY!!

M O T I V A T E!
 
The table myenigmaself pointed you to indicates that normal keys only return a single character. However, function keys (or other special keys) return two characters. Whenever two characters are returned, the first one will always be a NULL character, represented by numeric zero (0) (or by this &quot;character&quot; '\0').

So using getch() as 'Rod says, test the first returned character to see if it's a null. If it isn't, then the user pressed a normal character. If it *is* null, there is another character in the input buffer that you must read to find out which special key was pressed. In the following example, the &quot;else&quot; statements really are unneeded (unless you want to do something special), but are included to make things clearer:

char cMyChar;

/* program stops until a key is pressed */
cMyChar = getch();

if (cMyChar == 0)
{
/* first character was a NULL, so there is another */
/* character in the input buffer... let's read it */
cMyChar = getch();

if (cMyChar == 64)
{
/* In this example, F6 is the two character */
/* sequence (0, 64) you are looking for, so */
/* go ahead and run your batch command */
system(&quot;run&quot;);
}
else
{
/* a different special character was pressed, */
/* so ignore it and continue booting... */
}
}
else
{
/* a normal character was pressed, */
/* so ignore it and continue booting... */
}

Hope this helps... However, just be aware that using the getch() function requires the user to press *some* key even if they don't want to run the batch file. Also, in this example, the batch file will *only* be run if the user presses the F6 key.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top