Trouble with "left" and "right" keystroke

Its 5 files (2 headers and 3 .cpp)

This one is where the keystrokes are found (name: display (2).h):
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
#include <windows.h>
#include <cstdlib>
#include <string>
#include <stdio.h>
#include <conio.h>  /* needed for kbhit and getch */
#include <iostream>
using namespace std;

class Display
{
public:
	string *asciidone;
	int x,y;
	int min,max;
	int len;
	Display();
	void movecursor(int x, int y)
	{
		COORD coord = {x,y};
		SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
	}
	void displayascii()
	{
		system("cls");
		movecursor(x,y);
		cout << asciidone[min] << endl << flush;
		for(int j=min+1;j<max;j++)
			{
				movecursor(x,y+j);
				cout << asciidone[j] << endl << flush;
			}
		movecursor(x,y);
	}
	void displayasciimove()
	{
		if(max<=len){
			int i, j;
			system("cls");
			movecursor(x,y);
			cout << asciidone[min] << endl << flush;
			for(i=0,j=min+1;j<max;j++,i++)
				{
					y=y+1;
					movecursor(x,y);
					cout << asciidone[j] << endl << flush;
				}
			movecursor(x,y);
		}
	}
	void movecursordown()
	{ 
		y=y+1;
		if(y>=20)
		{
			y=10;
			min++;
			max++;
			displayasciimove();
		}
		else
		{
			movecursor(x,y);
		}
	}
	void movecursorup()
	{ 
		y=y-1;

		if(min == 0 && y <= 10)
			y = 10;
		else if(y<10)
		{
			min--;
			max--;
			y=10;
			displayasciimove();
			y=10;
			movecursor(x,y);
		}
		else
		{
			movecursor(x,y);
		}
	}
	struct kbBtn
	{
	   kbBtn() {}
	   kbBtn(unsigned char ch1, unsigned char ch2 = 0)
	   {
			  this->ch1 = ch1;
			  this->ch2 = ch2;

			  unsigned short uni = 256*ch1 | ch2;
			  //printf("uni = %0x\n", uni);
			  switch(uni)
			  {
			  case 0xE048:
				 strcpy(Name, "ArrDW");
				 break;
			  case 0xE04D:
				 strcpy(Name, "ArrRI");
				 break;
			  case 0xE050:
				 strcpy(Name, "ArrUP");
				 break;
			  case 0xE04B:
				 strcpy(Name, "ArrLF");
				 break;
			  case 0xE053:
				 strcpy(Name, "DEL");
				 break;
			  case 0xE049:
				 strcpy(Name, "PgUP");
				 break;
			  case 0xE051:
				 strcpy(Name, "PgDN");
				 break;
			  case 0xE04F:
				 strcpy(Name, "END");
				 break;
			  case 0xE047:
				 strcpy(Name, "HOME");
				 break;
			  case 0x0800:
				 strcpy(Name, "BckSp");
				 break;
			  case 0x1A00:
				 strcpy(Name, "CtrlZ");
				 break;
			  default:
				 strcpy(Name, "ALFNUM");
				 break;
			  }
		   }
		   char ch1, ch2;
		   char Name[10];
	};
	int start()
	{
		int  ch1, ch2;
		kbBtn  btn;
    
		ch1 = -1;
		while (ch1 != 0x1B)              /* watch for an ESC */
			{
				if (kbhit())                /* check for a keystroke */
				{
					ch1 = getch();           /* get the key */
					if (ch1 == 0 || ch1 == 0xE0)            /* if it's 0... */
					{
					   ch2 = getch();       /* get the next key */
					   btn = getStrike(ch1, ch2);
					}
				}
				Sleep(200);
			}
		return 0;
	}
	kbBtn getStrike(unsigned char ch1, unsigned char ch2)
	{
	   kbBtn btn(ch1,ch2);
	   if(strcmp(btn.Name,"ArrDW"))
		   movecursordown();
	   else if(strcmp(btn.Name,"ArrUP"))
		   movecursorup();
	   else if(strcmp(btn.Name,"ArrRI"))
	   {
		   x = x+1;
		   movecursor(x,y);
	   }
	   return btn;
	}
};


Heres the cpp for that header (name: display.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include<windows.h>
#include <stdio.h>
#include "display (2).h"
using namespace std;

Display::Display()
{
	  x = 5;
	  y = 10;
	  movecursor(x,y);
}


Heres the derived class (name: document.h):
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "display (2).h"
using namespace std;

class Document : public Display
{
	
public:
	Document();
	~Document();
	int getRow(int nr);
};


And its cpp (name: document.cpp):
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
#include <iostream>
#include <string>
#include <windows.h>
#include <stdio.h>
#include "document.h"
using namespace std;

#define AINDEX 10

Document::Document()
{
	string *ascii;
	int start = 48;
	int i;
	int incre=AINDEX;
	int doit = 1;
	int startfrom=0;

	ascii = new string[AINDEX];
	asciidone = new string[AINDEX];

	min = 0;
	max = 10;
	do
	{
		for (i=startfrom;i<incre; i++)
		{
			if(start == 123)
			{
				doit = 0;
			}
			else
			{
				for(int j=0;j<50;j++)
				{
					asciidone[i] += start;
				}
				start++;
			}
		}
		for(i=0;i<incre;i++)
		{
			ascii[i] = asciidone[i];
		}
		incre += AINDEX;
		delete[] asciidone;
		asciidone = new string[incre];
		for(i=0;i<incre-10;i++)
		{
			asciidone[i] = ascii[i];
		}
		delete[] ascii;
		ascii = new string[incre];
		startfrom = incre - 10;
	} while (doit);

	len = start - 48;

	displayascii();
	delete[] ascii;
}

Document::~Document()
{
	delete[] asciidone;
}


the main:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
#include "document.h"
using namespace std;

#define AINDEX 10

int main() {
	Document d;
	d.start();
}


Anyone can figure out how to implement the left and right buttons to work??
Just left, right? the other ones work?
I cannot test your program, however
144
145
146
147
148
149
150
151
152
153
154
155
156
while (ch1 != 0x1B)              /* watch for an ESC */
	{
		if (kbhit())                /* check for a keystroke */
		{
			ch1 = getch();           /* get the key */
			if (ch1 == 0 || ch1 == 0xE0)            /* if it's 0... */
			{
			   ch2 = getch();       /* get the next key */
			   btn = getStrike(ch1, ch2);
			}
		}
		Sleep(200); //IIRC: you need to hit the key between the pauses (quite hard)
	}
http://msdn.microsoft.com/en-us/library/ms927178.aspx use the enums instead of the hexadecimal value.
Topic archived. No new replies allowed.