Ik denk dat de oplossing toch in deze post zit
Ik heb namelijk dit uit het eerste resultaat uitgevoerd:
code:
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
| kbhit() is not available on Linux (UNIX), it is a DOS function.
Below is some code (functions) to replace the "kbhit" functionality (this
code is from a book called "Beginning Linux Programming", from Wrox Press
-- www.wrox.com -- you can download the Code Examples from the Book at
this Site):
CCODE
/********************************************************************/
KBHIT.H
#ifndef KBHITh
#define KBHITh
void init_keyboard(void);
void close_keyboard(void);
int kbhit(void);
int readch(void);
#endif
KBHIT.C
#include "kbhit.h"
#include <termios.h>
#include <unistd.h> // for read()
static struct termios initial_settings, new_settings;
static int peek_character = -1;
void init_keyboard()
{
tcgetattr(0,&initial_settings);
new_settings = initial_settings;
new_settings.c_lflag &= ~ICANON;
new_settings.c_lflag &= ~ECHO;
new_settings.c_lflag &= ~ISIG;
new_settings.c_cc[VMIN] = 1;
new_settings.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &new_settings);
}
void close_keyboard()
{
tcsetattr(0, TCSANOW, &initial_settings);
}
int kbhit()
{
unsigned char ch;
int nread;
if (peek_character != -1) return 1;
new_settings.c_cc[VMIN]=0;
tcsetattr(0, TCSANOW, &new_settings);
nread = read(0,&ch,1);
new_settings.c_cc[VMIN]=1;
tcsetattr(0, TCSANOW, &new_settings);
if(nread == 1)
{
peek_character = ch;
return 1;
}
return 0;
}
int readch()
{
char ch;
if(peek_character != -1)
{
ch = peek_character;
peek_character = -1;
return ch;
}
read(0,&ch,1);
return ch;
} |
De functie kbhit() heb ik dus in mijn file gezet, en ik controleer met
of er nog niet is gedrukt.
Nu heb ik nog 2 problemen:
- Hoe kan ik controleren of er op de q is gedrukt en niet op een andere toets?
(- Hoe zorg ik ervoor dat na het uitvoeren van het programma de instellingen van de terminal weer normaal zijn, door die kbhit word namelijk ingesteld dat er geen enters meer voorkomen in de terminal en ook word er niks meer geecho'ed. Dat is dus erg lastig met een opdracht intypen, omdat je dus niks ziet behalve:
[code][user@localhost map]#
[/code])
Al opgelost, door de functies init_keyboard(); en close_keyboard(); toe te voegen

dom,dom (had alleen close_keyboard gedaan)
Nu dus alleen probleem 1 nog.
[
Voor 13% gewijzigd door
RHE123 op 04-01-2004 12:30
]