How to monitor key stroke ?

C, C++, Visual C++, C++.Net Topics
Post Reply
RAT16F88
Lieutenant
Lieutenant
Posts: 72
Joined: Sat Nov 20, 2010 12:36 pm
Location: Inside a PIC

How to monitor key stroke ?

Post by RAT16F88 » Tue Jun 28, 2011 10:37 am

Dear EXPERTS,
In a program with a loop like >

Code: Select all

#include <cstdlib>
#include <iostream>
#include <Windows.h>
using namespace std;

int i ; 

int main(int argc, char *argv[])
{
   do{
    i = rand();
    printf("%d \n", i ) ;
    Sleep(1000);
    Beep(3000,50);
    } while (1);
    system("PAUSE");
    return EXIT_SUCCESS;
}



How to add a line in the loop to exit it with a key stroke ? :idea: :?: :?:
thanks and regards :) :) :)
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: How to monitor key stroke ?

Post by Neo » Tue Jun 28, 2011 11:18 am

If you are using Visual C++, check this out. The trick is _kbhit() under conio.h.

ADVICE: Please maintain indents (tabs) which is a good programming practice. Notice the tabs I maintained for the loop.

Code: Select all

#include <stdio.h>
#include <conio.h>
#include <Windows.h>


int main(int argc, char *argv[])
{
	int i;

	do{
		i = rand();
		printf("%d \n", i ) ;
		Sleep(1000);
		Beep(3000,50);
	} while (!_kbhit());

	system("PAUSE");
	return EXIT_SUCCESS;
}
If you are using Dev C++, you need to use something as follows. I don't have the IDE to test, so you need to play a bit with it.

Code: Select all

#include <windows.h>

int main(void )
{
	keybd_event(VkKeyScan('y'), 0, 0, 0);
	keybd_event(VkKeyScan('y'), 0, KEYEVENTF_KEYUP, 0);
	keybd_event(VkKeyScan('q'), 0, 0, 0);
	keybd_event(VkKeyScan('q'), 0, KEYEVENTF_KEYUP, 0);

	// or alternatively...
		
	keybd_event(0x59, 0, 0, 0);
	keybd_event(0x59, 0, KEYEVENTF_KEYUP, 0);
	keybd_event(0x51, 0, 0, 0);
	keybd_event(0x51, 0, KEYEVENTF_KEYUP, 0);	
	return 0;
}
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: How to monitor key stroke ?

Post by Face » Tue Jun 28, 2011 11:08 pm

Ahhh.Nice Idea.I was thinking about this program.

With this we can get any ones passwords usernames etc..that was the idea I was thinking.This is a really easy way to steal those passwords.If we can make this program to get these key strokes to notepad file or any.we can get some fun on that.

(Hope NEO & others will not misunderstand me & my crazy ideas :D That was the way I got this thing in my mind few months ago.This is nothing to harm any one,Just a idea.:D )
User avatar
Rksk
Major
Major
Posts: 730
Joined: Thu Jan 07, 2010 4:19 pm
Location: Rathnapura, Sri Lanka

Re: How to monitor key stroke ?

Post by Rksk » Tue Jun 28, 2011 11:20 pm

Ones I tried to make a key logger (Only for learning). Kaspersky detected it as a suspecious activity at the 1st ran. I think, there isn't any antivirus programe like Kaspersky witch detects activities of programes.

[ Post made via Mobile Device ] Image
User avatar
Enigma
Lieutenant
Lieutenant
Posts: 74
Joined: Sun Jan 16, 2011 12:40 am
Location: Colombo, Sri Lanka

Re: How to monitor key stroke ?

Post by Enigma » Sun Jul 03, 2011 9:08 pm

Hi
Here is another way. I used GetAsyncKeyState(int key) which is a system level API . The link below will give you more details about it. Guys this is a very common method. So its not that stealthy. Kaspersky (I used kaspersky 2011) can detect this ;) . Note that this function can trace keys globally. So even this program is minimized it can detect key strokes .

GetAsyncKeyState(int key) - http://msdn.microsoft.com/en-us/library ... 85%29.aspx

Code: Select all

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

int get_keys(void);

int main()
{
	 get_keys();
	return 0;
}


int get_keys(void)
{
short character;
char* key =NULL;
	while(1)
	{
		
		for(character=8;character<=200;character++)
		{
			if(GetAsyncKeyState(character)==0xFFFF8001) 
			{
				if((character>=39)&&(character<91))
				{	
					printf("%c pressed\n",character);
					break;
				}
				else
				{
					switch(character)
					{
						case VK_SPACE:
						printf("[SPACE] pressed\n");
						break;

						case VK_SHIFT:
						printf("[SHIFT] pressed\n");
						break;

						case VK_RETURN:
						printf("[ENTER] pressed\n");
						break;

						case VK_BACK:
						printf("[BACKSPACE] pressed\n");
						break;

						case VK_TAB:
						printf("[TAB] pressed\n");
						break;

						case VK_CONTROL:
						printf("[CTRL] pressed\n");
						break;

						case VK_DELETE:
						printf("[DEL] pressed\n");
						break;

						case VK_NUMPAD0:
						printf("0 pressed\n");
						break;

						case VK_NUMPAD1:
						printf("1 pressed\n");
						break;

						case VK_NUMPAD2:
						printf("2 pressed\n");
						break;

						case VK_NUMPAD3:
						printf("3 pressed\n");
						break;

						case VK_NUMPAD4:
						printf("4 pressed\n");
						break;

						case VK_NUMPAD5:
						printf("5 pressed\n");
						break;

						case VK_NUMPAD6:
						printf("6 pressed\n");
						break;

						case VK_NUMPAD7:
						printf("7 pressed\n");
						break;

						case VK_NUMPAD8:
						printf("8 pressed\n");
						break;

						case VK_NUMPAD9:
						printf("9 pressed\n");
						break;

						case VK_CAPITAL:
						printf("[CAPS LOCK] pressed\n");
						break;

						default:
						break;
					}
				}
			}
		}
	}
return 0;
}
Post Reply

Return to “C/C++ Programming”