main.c
#include <stdio.h> #include <stdlib.h> #include <SDL.h> #include <SDL_gfxPrimitives.h> #define SCALE_W 3.4 #define SCALE_H 3.4 SDL_Surface *screen = NULL; int key_map[1024] = {0}; int key_status[5 * 12] = {0}; char key_name[5 * 12][32] = { "ESC", " 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", " 0", " BS", " Q", " W", " E", " R", " T", " Y", " U", " I", " O", " P", "CTRL", "", " \\", " A", " S", " D", " F", " G", " H", " J", " K", " L", "ENTER", "", "SHIFT", " Z", " X", " C", " V", " B", " N", " M", " ;", " UP", "SHIFT", "", " ,", "DEL", " -", " =", "TAB", "SPACE", " .", " /", "LEFT", "DOWN", "RIGHT", "", }; int draw_bg(void) { int x = 0, y = 0; SDL_Rect rt = {0}; uint32_t c_rel = 0xf800; int rows[] = {12, 11, 11, 11, 11}; int cols[5 * 12] = { 11, 11, 11, 10, 10, 10, 10, 10, 10, 11, 11, 12, 18, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 0, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 19, 0, 17, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 0, 10, 10, 10, 10, 10, 24, 10, 10, 11, 11, 12, 0, }; int xx = 0; key_status[0] = 0; for (y = 0; y < 5; y++) { xx = 0; for (x = 0; x < rows[y]; x++) { rt.x = 2 + xx; rt.y = 16 + (11 + 1) * y; rt.w = cols[(y * 12) + x]; rt.h = 11; xx+= cols[(y * 12) + x] + 1; rt.x = (float)rt.x * SCALE_W; rt.y = (float)rt.y * SCALE_H; rt.w = (float)rt.w * SCALE_W; rt.h = (float)rt.h * SCALE_H; SDL_FillRect(screen, &rt, key_status[(y * 12) + x] ? 0xf800 : 0xffff); stringRGBA(screen, rt.x + 3, rt.y + 15, key_name[(y * 12) + x], 0x00, 0x00, 0x00, 0xff); } } stringRGBA(screen, 10, 27, "iriver Dicple D88", 0xff, 0xff, 0x00, 0xff); } int main(int argc, char* args[]) { int cc = 0; key_map[SDLK_ESCAPE] = 0 * 12 + 0; key_map[SDLK_1] = 0 * 12 + 1; key_map[SDLK_2] = 0 * 12 + 2; key_map[SDLK_3] = 0 * 12 + 3; key_map[SDLK_4] = 0 * 12 + 4; key_map[SDLK_5] = 0 * 12 + 5; key_map[SDLK_6] = 0 * 12 + 6; key_map[SDLK_7] = 0 * 12 + 7; key_map[SDLK_8] = 0 * 12 + 8; key_map[SDLK_9] = 0 * 12 + 9; key_map[SDLK_0] = 0 * 12 + 10; key_map[SDLK_BACKSPACE] = 0 * 12 + 11; key_map[SDLK_q] = 1 * 12 + 0; key_map[SDLK_w] = 1 * 12 + 1; key_map[SDLK_e] = 1 * 12 + 2; key_map[SDLK_r] = 1 * 12 + 3; key_map[SDLK_t] = 1 * 12 + 4; key_map[SDLK_y] = 1 * 12 + 5; key_map[SDLK_u] = 1 * 12 + 6; key_map[SDLK_i] = 1 * 12 + 7; key_map[SDLK_o] = 1 * 12 + 8; key_map[SDLK_p] = 1 * 12 + 9; key_map[SDLK_LCTRL] = 1 * 12 + 10; key_map[SDLK_BACKSLASH] = 2 * 12 + 0; key_map[SDLK_a] = 2 * 12 + 1; key_map[SDLK_s] = 2 * 12 + 2; key_map[SDLK_d] = 2 * 12 + 3; key_map[SDLK_f] = 2 * 12 + 4; key_map[SDLK_g] = 2 * 12 + 5; key_map[SDLK_h] = 2 * 12 + 6; key_map[SDLK_j] = 2 * 12 + 7; key_map[SDLK_k] = 2 * 12 + 8; key_map[SDLK_l] = 2 * 12 + 9; key_map[SDLK_RETURN] = 2 * 12 + 10; key_map[SDLK_LSHIFT] = 3 * 12 + 0; key_map[SDLK_z] = 3 * 12 + 1; key_map[SDLK_x] = 3 * 12 + 2; key_map[SDLK_c] = 3 * 12 + 3; key_map[SDLK_v] = 3 * 12 + 4; key_map[SDLK_b] = 3 * 12 + 5; key_map[SDLK_n] = 3 * 12 + 6; key_map[SDLK_m] = 3 * 12 + 7; key_map[SDLK_SEMICOLON] = 3 * 12 + 8; key_map[SDLK_UP] = 3 * 12 + 9; key_map[SDLK_COMMA] = 4 * 12 + 0; key_map[SDLK_DELETE] = 4 * 12 + 1; key_map[SDLK_MINUS] = 4 * 12 + 2; key_map[SDLK_EQUALS] = 4 * 12 + 3; key_map[SDLK_TAB] = 4 * 12 + 4; key_map[SDLK_SPACE] = 4 * 12 + 5; key_map[SDLK_PERIOD] = 4 * 12 + 6; key_map[SDLK_SLASH] = 4 * 12 + 7; key_map[SDLK_LEFT] = 4 * 12 + 8; key_map[SDLK_DOWN] = 4 * 12 + 9; key_map[SDLK_RIGHT] = 4 * 12 + 10; SDL_Init(SDL_INIT_VIDEO); screen = SDL_SetVideoMode(480, 272, 16, SDL_SWSURFACE); SDL_ShowCursor(0); SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00)); draw_bg(); SDL_Flip(screen); int redraw = 0; SDL_Event event; while (1) { redraw = 0; if (SDL_PollEvent(&event)) { if (event.type == SDL_KEYDOWN) { redraw = 1; key_status[key_map[event.key.keysym.sym]] = 1; if (event.key.keysym.sym == SDLK_LSHIFT) { key_status[3 * 12 + 10] = 1; } if (event.key.keysym.sym == SDLK_ESCAPE) { break; } } if (event.type == SDL_KEYUP) { redraw = 1; key_status[key_map[event.key.keysym.sym]] = 0; if (event.key.keysym.sym == SDLK_LSHIFT) { key_status[3 * 12 + 10] = 0; } } if (redraw) { draw_bg(); SDL_Flip(screen); } } SDL_Delay(30); } SDL_Quit(); return 0; }
Makefile
CROSS = mipsel-linux- all: $(CROSS)gcc main.c -o kbd2 -I/opt/retrofw/mipsel-buildroot-linux-uclibc/sysroot/usr/include/SDL -lSDL -lSDL_image -lSDL_gfx clean: rm -rf kbd2
這鍵盤設計真有問題,卡鍵問題太嚴重,而且除了CTRL、SHIFT,其餘按鍵也無法同時按下...