feat: Dedicated key codes for arrow key movement.
This commit is contained in:
@@ -12,9 +12,17 @@
|
||||
|
||||
/*** defines ***/
|
||||
|
||||
#define CTRL_KEY(k) ((k) & 0x1f)
|
||||
#define KILO_VERSION "0.0.1"
|
||||
|
||||
#define CTRL_KEY(k) ((k) & 0x1f)
|
||||
|
||||
enum editorKey {
|
||||
ARROW_LEFT = 1000,
|
||||
ARROW_RIGHT,
|
||||
ARROW_UP,
|
||||
ARROW_DOWN
|
||||
};
|
||||
|
||||
/*** data ***/
|
||||
|
||||
struct editorConfig {
|
||||
@@ -63,7 +71,7 @@ void enableRawMode() {
|
||||
die("tcsetattr");
|
||||
}
|
||||
|
||||
char editorReadKey() {
|
||||
int editorReadKey() {
|
||||
int nread;
|
||||
char c;
|
||||
while ((nread = read(STDIN_FILENO, &c, 1)) != 1) {
|
||||
@@ -83,10 +91,10 @@ char editorReadKey() {
|
||||
|
||||
if (seq[0] == '[') {
|
||||
switch (seq[1]) {
|
||||
case 'A': return 'w';
|
||||
case 'B': return 's';
|
||||
case 'C': return 'd';
|
||||
case 'D': return 'a';
|
||||
case 'A': return ARROW_UP;
|
||||
case 'B': return ARROW_DOWN;
|
||||
case 'C': return ARROW_RIGHT;
|
||||
case 'D': return ARROW_LEFT;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -225,25 +233,25 @@ void editorRefreshScreen() {
|
||||
|
||||
/*** input ***/
|
||||
|
||||
void editorMoveCursor(char key) {
|
||||
void editorMoveCursor(int key) {
|
||||
switch (key) {
|
||||
case 'a':
|
||||
case ARROW_LEFT:
|
||||
E.cx--;
|
||||
break;
|
||||
case 'd':
|
||||
case ARROW_RIGHT:
|
||||
E.cx++;
|
||||
break;
|
||||
case 'w':
|
||||
case ARROW_UP:
|
||||
E.cy--;
|
||||
break;
|
||||
case 's':
|
||||
case ARROW_DOWN:
|
||||
E.cy++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void editorProcessKeypress() {
|
||||
char c = editorReadKey();
|
||||
int c = editorReadKey();
|
||||
|
||||
switch (c) {
|
||||
case CTRL_KEY('q'):
|
||||
@@ -254,10 +262,10 @@ void editorProcessKeypress() {
|
||||
|
||||
exit(0);
|
||||
break;
|
||||
case 'w':
|
||||
case 'a':
|
||||
case 's':
|
||||
case 'd':
|
||||
case ARROW_UP:
|
||||
case ARROW_LEFT:
|
||||
case ARROW_DOWN:
|
||||
case ARROW_RIGHT:
|
||||
editorMoveCursor(c);
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user