Various keyboard handling improvements

The Ctrl, Alt and Shift bits in Controller/button activate with either
left or right modifier keys, and either can be used to take screenshots
/ zoom / show debugging info rather than left modifiers only.

Delete now sets Controller/key to 0x7f rather than being ignored (by
setting Controller/key to 0x00).

Ctrl + letter now sets Controller/key to 0x01-0x1e rather than being
ignored: for example, Ctrl+S will set Controller/key to 0x13.

Controller/vector is no longer called twice per key press (the extra
call with Controller/key set to 0x00 is removed).
This commit is contained in:
Andrew Alderwick 2021-06-19 13:56:55 +01:00
parent 7fe0750c17
commit c268c7fd96
1 changed files with 25 additions and 24 deletions

View File

@ -145,7 +145,6 @@ init(void)
return error("Texture", SDL_GetError()); return error("Texture", SDL_GetError());
SDL_UpdateTexture(bgTexture, NULL, ppu.bg.pixels, 4); SDL_UpdateTexture(bgTexture, NULL, ppu.bg.pixels, 4);
SDL_UpdateTexture(fgTexture, NULL, ppu.fg.pixels, 4); SDL_UpdateTexture(fgTexture, NULL, ppu.fg.pixels, 4);
SDL_StartTextInput();
SDL_ShowCursor(SDL_DISABLE); SDL_ShowCursor(SDL_DISABLE);
SDL_zero(as); SDL_zero(as);
as.freq = SAMPLE_FREQUENCY; as.freq = SAMPLE_FREQUENCY;
@ -187,34 +186,40 @@ void
doctrl(Uxn *u, SDL_Event *event, int z) doctrl(Uxn *u, SDL_Event *event, int z)
{ {
Uint8 flag = 0x00; Uint8 flag = 0x00;
if(z && event->key.keysym.sym == SDLK_h) SDL_Keymod mods = SDL_GetModState();
switch(SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL | KMOD_LALT)) { devctrl->dat[2] &= 0xf8;
case KMOD_LSHIFT | KMOD_LCTRL: if(mods & KMOD_CTRL) devctrl->dat[2] |= 0x01;
screencapture(); if(mods & KMOD_ALT) devctrl->dat[2] |= 0x02;
break; if(mods & KMOD_SHIFT) devctrl->dat[2] |= 0x04;
case KMOD_LCTRL:
toggledebug(u);
break;
case KMOD_LALT:
togglezoom(u);
break;
}
switch(event->key.keysym.sym) { switch(event->key.keysym.sym) {
case SDLK_LCTRL: flag = 0x01; break; case SDLK_h:
case SDLK_LALT: flag = 0x02; break; if(z) switch(devctrl->dat[2] & 0x07) {
case SDLK_LSHIFT: flag = 0x04; break; case 0x1: toggledebug(u); break;
case 0x2: togglezoom(u); break;
case 0x5: screencapture(); break;
}
break;
case SDLK_ESCAPE: flag = 0x08; break; case SDLK_ESCAPE: flag = 0x08; break;
case SDLK_UP: flag = 0x10; break; case SDLK_UP: flag = 0x10; break;
case SDLK_DOWN: flag = 0x20; break; case SDLK_DOWN: flag = 0x20; break;
case SDLK_LEFT: flag = 0x40; break; case SDLK_LEFT: flag = 0x40; break;
case SDLK_RIGHT: flag = 0x80; break; case SDLK_RIGHT: flag = 0x80; break;
} }
if(flag && z) if(z)
devctrl->dat[2] |= flag; devctrl->dat[2] |= flag;
else if(flag) else
devctrl->dat[2] &= (~flag); devctrl->dat[2] &= ~flag;
if(z && event->key.keysym.sym < 20) if(z && event->key.keysym.sym < 0x80) {
devctrl->dat[3] = event->key.keysym.sym; devctrl->dat[3] = event->key.keysym.sym;
if(devctrl->dat[3] >= 0x60 && devctrl->dat[3] < 0x7f) {
if(devctrl->dat[2] & 0x01)
devctrl->dat[3] &= ~0x60;
else if(!(devctrl->dat[2] & 0x04) ^ !(mods & KMOD_CAPS))
devctrl->dat[3] &= ~0x20;
}
} else {
devctrl->dat[3] = 0;
}
} }
#pragma mark - Devices #pragma mark - Devices
@ -359,14 +364,10 @@ start(Uxn *u)
case SDL_QUIT: case SDL_QUIT:
quit(); quit();
break; break;
case SDL_TEXTINPUT:
case SDL_KEYDOWN: case SDL_KEYDOWN:
case SDL_KEYUP: case SDL_KEYUP:
if(event.text.text[0] >= ' ' && event.text.text[0] <= '~')
devctrl->dat[3] = event.text.text[0];
doctrl(u, &event, event.type == SDL_KEYDOWN); doctrl(u, &event, event.type == SDL_KEYDOWN);
evaluxn(u, mempeek16(devctrl->dat, 0)); evaluxn(u, mempeek16(devctrl->dat, 0));
devctrl->dat[3] = 0;
break; break;
case SDL_MOUSEWHEEL: case SDL_MOUSEWHEEL:
devmouse->dat[7] = event.wheel.y; devmouse->dat[7] = event.wheel.y;