1,105,352 Community Members

Reading Scan Codes from the Keyboard

Member Avatar
balucec
Newbie Poster
3 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi all,
I need a method to intercept the key before it reaches my application
ie, I need to trap the keys pressed and convert it to my local language, Malayalam, and then display it in my app
thanks and regards

Member Avatar
balucec
Newbie Poster
3 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi all,
I need a method(in C language) to intercept the key before it reaches my application
ie, I need to trap the keys pressed and convert it to my local language, Malayalam, and then display it in my app
thanks and regards

in C language

Member Avatar
Ancient Dragon
Achieved Level 70
27,580 posts since Aug 2005
Reputation Points: 5,231 [?]
Q&As Helped to Solve: 3,028 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

you will have to use Windows Hooks to do that

Member Avatar
pritamprasad
Newbie Poster
1 post since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

can ne1 plz explain about using the code (keystroke == -32)... how it is implemenbted here...?

Member Avatar
roshan_iiita
Light Poster
30 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

But this doesn't work on Linux. How can we detect the keys pressed on Linux ?

Member Avatar
japun
Newbie Poster
1 post since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

How can i trap a user input using ascii code?
Wherein the user should input integer only ranging (0-99),
using loop structures.

Help me pls.. I'm fresh I.T student..

Member Avatar
Ancient Dragon
Achieved Level 70
27,580 posts since Aug 2005
Reputation Points: 5,231 [?]
Q&As Helped to Solve: 3,028 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

The only way I know how is to use either termcap library or curses. Unlike MS-Windows, *nix supports a huge variety of monitory, keyboards, and terminals.

Member Avatar
Zvjezdan23
Junior Poster
153 posts since Dec 2010
Reputation Points: -13 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

That code didn't work for me so I modified it. This code will work for any situation that someone needs to use the up arrow, down arrow, left arrow, and right arrow.

#include <iostream>
#include <string>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <conio.h>
#include <windows.h>
#include <stdio.h>

#define PAGE_UP     73
#define HOME        71
#define END         79
#define PAGE_DOWN   81
#define UP_ARROW    72
#define LEFT_ARROW  75
#define DOWN_ARROW  80
#define RIGHT_ARROW 77
#define F1          59
#define F2          60
#define F3          61
#define F4          62
#define F5          63
#define F6          64
#define F7          65
#define F8          66
#define F9          67
#define F10         68

using namespace std;

int gotoxy(int x, int y) // used to make window
{  
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD point;
   	point.X = x-1;
	point.Y = y-1;     
   	SetConsoleCursorPosition(hConsole, point);
   	return SetConsoleCursorPosition(hConsole, point);
}

void object()
{
	cout << "7" << endl;
}

int main()
{
	system("mode 80,40");

	char ch_, key, key_, ch;
	char up, down, left, right;


	a: cout << "Press up arrow key: ";
	ch_ = getch();

	if (kbhit())
	{
		ch_ = getch();

		if (ch_ == UP_ARROW)
		{
			ch_ = UP_ARROW;
			up = ch_;
			cout << up << endl;
		}

		else if (ch_ != UP_ARROW)
		{
			cout << "Not the up arrow key" << endl;
			system("pause");
			system("cls");
			goto a;
		}
	}

	b: cout << "Press down arrow key: ";
	key = getch();

	if (kbhit())
	{
		key = getch();

		if (key == DOWN_ARROW)
		{
			key = DOWN_ARROW;
			down = key;
			cout << down << endl;
		}

		else if (key != DOWN_ARROW)
		{
			cout << "Not the down arrow key" << endl;
			system("pause");
			system("cls");
			goto b;
		}
	}

	c: cout << "Press left arrow key: ";
	key_ = getch();

	if (kbhit())
	{
		key_ = getch();

		if (key_ == LEFT_ARROW)
		{
			key_ = LEFT_ARROW;
			left = key_;
			cout << left << endl;
		}

		else if (key_ != LEFT_ARROW)
		{
			cout << "Not the left arrow key" << endl;
			system("pause");
			system("cls");
			goto c;
		}
	}

	d: cout << "Press right arrow key: ";
	ch_ = getch();

	if (kbhit())
	{
		ch_ = getch();

		if (ch_ == RIGHT_ARROW)
		{
			ch_ = RIGHT_ARROW;
			right = ch_;
			cout << right << endl;
		}

		else if (ch_ != RIGHT_ARROW)
		{
			cout << "Not the right arrow key" << endl;
			system("pause");
			system("cls");
			goto d;
		}
	}

	system("cls");

	int object_x = 15;
	int object_y = 20;

	gotoxy(object_x,object_y);
	object();

	int x = 1;
	int y = 1;

	char movement;

	for (int i = 0; i < 10; i++)
	{
	gotoxy(3,30);
	cout << "Enter movement key: ";
	movement = getch();
	gotoxy(3,30);
	cout << "                                      " << endl;

	if(kbhit())
	{
		movement = getch();

		if(movement == up)
		{
			int go_up = (object_y - y);
			object_y = go_up;

			gotoxy(object_x,object_y);
			object();
			gotoxy(3,30);
			cout << "You moved north" << endl;
		}

		else if (movement == down)
		{
			int go_down = (object_y + y);
			object_y = go_down;

			gotoxy(object_x, object_y);
			object();
			gotoxy(3,30);
			cout << "You went down" << endl;
		}

		else if (movement == left)
		{
			int go_left = (object_x - x);
			object_x = go_left;

			gotoxy(object_x,object_y);
			object();
			gotoxy(3,30);
			cout << "You went left" << endl;
		}

		else if (movement == right)
		{
			int go_right = (object_x + x);
			object_x = go_right;

			gotoxy(object_x,object_y);
			object();
			gotoxy(3,30);
			cout << "You went right" << endl;
		}
	}
	}

	system("pause");
	return 0;
}
Member Avatar
Zvjezdan23
Junior Poster
153 posts since Dec 2010
Reputation Points: -13 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

make the gotoxy(3,30); for you went north, left, down, right to gotoxy(3,29); and in line 164 make that also gotoxy(3,29);

Member Avatar
Ancient Dragon
Achieved Level 70
27,580 posts since Aug 2005
Reputation Points: 5,231 [?]
Q&As Helped to Solve: 3,028 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

That code didn't work for me so I modified it. This code will work for any situation that someone needs to use the up arrow, down arrow, left arrow, and right arrow.

The problem with your program is that there is no difference between F1 and a semicolon because they both have the same ascii value. Same with all the other special keys you defined. That's why I added 255 to the ascii value for special keys so that the program would be able to distinguish them from normal keys. Some programmers just make the value of special keys negative, e.g. #defind F1 (-59)

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

That code didn't work for me so I modified it. This code will work for any situation that someone needs to use the up arrow, down arrow, left arrow, and right arrow.

It will? Try it on any Linux Compiler. Try it on any non-Borland/Microsoft compiler. It won't work in most of these situations which means it doesn't work.

Member Avatar
Zvjezdan23
Junior Poster
153 posts since Dec 2010
Reputation Points: -13 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

What are you talking about??? I only posted this for up down left right arrows. Can you please explain what you mean??

Member Avatar
Zvjezdan23
Junior Poster
153 posts since Dec 2010
Reputation Points: -13 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

@waltp: My bad. I'm sorry. I'm new to programming so idk if there is any difference. I thought everything that's C, C++, C# is coded in visual studios

senusret_V
Newbie Poster
1 post since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

Hello i just signed up for this question. I noticed that it was an old post but I also am making a game application which is going to be a 2D soccer game. I need a KeyListener in C++ which will detect keystrokes immediately. For example, to move a radiobutton a little right when i press right arrow on keyboard. Is the code above can do it? I must detect key stroke immediately (not 1-console input and 2-press enter to continue kind of program) I would really appreciate if any of you can answer me. :)

Member Avatar
Ancient Dragon
Achieved Level 70
27,580 posts since Aug 2005
Reputation Points: 5,231 [?]
Q&As Helped to Solve: 3,028 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

It might, assuming you don't plan to port the program to another operating system and use a compiler that supports functions in conio.h. conio.h and its associated library are non-standard, which means not all compilers will support it. Too bad because conio.h contains several really useful functions, such as kbhit(). But you can replicate that by using win32 api console functions.

Member Avatar
thendrluca
Newbie Poster
20 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 
#include <stdio.h>
#include <conio.h>
int main()
{
    while (1)
        printf("%d\n", getch());

    return 0;   
}
You
Post:
Start New Discussion
Tags Related to this Article