Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void input(char* str, int x, int y, int width) {
- const unsigned int sc013A[] = { 0xD201D002, 0x422B0009, 0x80010070, 0x013A }; // syscall to display OS cursor
- #define Cursor_SetFlashMode ((void(*)(int))sc013A)
- #define INPUT_BUFFER_SIZE 1024
- char buffer[INPUT_BUFFER_SIZE];
- int run=1, key, i, cursor=0, length=0, displayStart=0;
- buffer[0] = 0;
- Cursor_SetFlashMode(1); // enable OS cursor
- while(run) {
- if (cursor && cursor <= displayStart) displayStart = cursor-1;
- if (cursor-displayStart >= width) displayStart = cursor-width+1;
- locate(x, y);
- PrintLine(buffer + displayStart, x+width-1); // display string
- if (length-displayStart < width) {
- locate(x+length-displayStart, y);
- Print(" "); // clear old last character in case of removal
- }
- locate(x+cursor-displayStart, y); // set position of displayed cursor
- GetKey(&key);
- switch (key) {
- case KEY_CTRL_LEFT:
- if (cursor) cursor--;
- break;
- case KEY_CTRL_RIGHT:
- if (cursor < length) cursor++;
- break;
- case KEY_CTRL_EXE:
- run = 0;
- break;
- case KEY_CTRL_DEL:
- if (cursor) cursor--;
- for (i=cursor ; i<length ; i++)
- buffer[i] = buffer[i+1];
- if (length) length--;
- break;
- default:
- if (key >= 32 && key < 127 && length+1 < INPUT_BUFFER_SIZE) { // if ASCII char
- for (i=length ; i>=cursor ; i--)
- buffer[i+1] = buffer[i];
- buffer[cursor] = key;
- cursor++;
- length++;
- }
- }
- }
- Cursor_SetFlashMode(0);
- for (i=0 ; i<=length ; i++) {
- str[i] = buffer[i];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement