feat: Dedicated key codes for arrow key movement.

This commit is contained in:
jmug
2026-10-26 12:21:26 -07:00
parent e27f9c08de
commit ef1f3c69ac
+24 -16
View File
@@ -12,9 +12,17 @@
/*** defines ***/ /*** defines ***/
#define CTRL_KEY(k) ((k) & 0x1f)
#define KILO_VERSION "0.0.1" #define KILO_VERSION "0.0.1"
#define CTRL_KEY(k) ((k) & 0x1f)
enum editorKey {
ARROW_LEFT = 1000,
ARROW_RIGHT,
ARROW_UP,
ARROW_DOWN
};
/*** data ***/ /*** data ***/
struct editorConfig { struct editorConfig {
@@ -63,7 +71,7 @@ void enableRawMode() {
die("tcsetattr"); die("tcsetattr");
} }
char editorReadKey() { int editorReadKey() {
int nread; int nread;
char c; char c;
while ((nread = read(STDIN_FILENO, &c, 1)) != 1) { while ((nread = read(STDIN_FILENO, &c, 1)) != 1) {
@@ -83,10 +91,10 @@ char editorReadKey() {
if (seq[0] == '[') { if (seq[0] == '[') {
switch (seq[1]) { switch (seq[1]) {
case 'A': return 'w'; case 'A': return ARROW_UP;
case 'B': return 's'; case 'B': return ARROW_DOWN;
case 'C': return 'd'; case 'C': return ARROW_RIGHT;
case 'D': return 'a'; case 'D': return ARROW_LEFT;
} }
} }
@@ -225,25 +233,25 @@ void editorRefreshScreen() {
/*** input ***/ /*** input ***/
void editorMoveCursor(char key) { void editorMoveCursor(int key) {
switch (key) { switch (key) {
case 'a': case ARROW_LEFT:
E.cx--; E.cx--;
break; break;
case 'd': case ARROW_RIGHT:
E.cx++; E.cx++;
break; break;
case 'w': case ARROW_UP:
E.cy--; E.cy--;
break; break;
case 's': case ARROW_DOWN:
E.cy++; E.cy++;
break; break;
} }
} }
void editorProcessKeypress() { void editorProcessKeypress() {
char c = editorReadKey(); int c = editorReadKey();
switch (c) { switch (c) {
case CTRL_KEY('q'): case CTRL_KEY('q'):
@@ -254,10 +262,10 @@ void editorProcessKeypress() {
exit(0); exit(0);
break; break;
case 'w': case ARROW_UP:
case 'a': case ARROW_LEFT:
case 's': case ARROW_DOWN:
case 'd': case ARROW_RIGHT:
editorMoveCursor(c); editorMoveCursor(c);
break; break;
} }