Switch window keyboard input
1 Introduction
We implemented the input focus switching of the keyboard, but even though the control console is activated
If we tap the keyboard, we will find that the character input is still in the previous Message box.
In this section we are going to input the contents of keyboard strokes into the switched window.
The idea of implementation is this: we configure an input queue for each task
When the window is activated, once there is keyboard input, the main process will first obtain the keyboard data.
Then determine which window is currently activated and find the process object corresponding to the running window.
Obtain its corresponding input queue through the process object, put the information from the keyboard into the input queue, and then activate the corresponding process object.
2.Code
Modify code
First is multi_task.h
struct TASK {
int sel, flags;
int priority;
int level;
struct FIFO8 fifo;
struct TSS32 tss;
};
The above change is to add a queue to obtain information input
The next thing to modify is the CMain function of the main process. Once a keyboard event occurs,
CMain will be activated and it can put the received characters into the queue of the corresponding window process.
void CMain(void) {
....
for(;;) {
....
else if (key_to == 0) {
if (keytable[data] != 0 && cursor_x < 144) {
boxfill8(shtMsgBox->buf, shtMsgBox->bxsize, COL8_FFFFFF,cursor_x,
28, cursor_x + 7, 43);
sheet_refresh(shtctl, shtMsgBox, cursor_x, 28, cursor_x+8, 44);
char buf[2] = {keytable[data], 0};
showString(shtctl, shtMsgBox, cursor_x, 28, COL8_000000, buf);
cursor_x += 8;
stop_task_A = 1;
boxfill8(shtMsgBox->buf, shtMsgBox->bxsize, cursor_c, cursor_x,
28, cursor_x + 7, 43);
sheet_refresh(shtctl, shtMsgBox, cursor_x, 28, cursor_x+8, 44);
}
} else {
task_sleep(task_a);
fifo8_put(&task_cons->fifo, data);
}
}
....
}
....
}
If the currently activated window is a Message Box, the characters are displayed directly in the text box.
Otherwise, we suspend the current main process
Then put the keyboard data into the queue corresponding to the console process through fifo8_put.
We must remember to suspend the main process, otherwise the main process will always occupy CPU resources.
As a result, the console process cannot run and the keyboard data cannot be processed in time.
Console process changes
void console_task(struct SHEET *sheet) {
....
else {
if (i == 0x0e && cursor_x > 8) {
boxfill8(sheet->buf, sheet->bxsize, COL8_000000, cursor_x,
28, cursor_x + 7, 43);
sheet_refresh(shtctl, sheet, cursor_x, 28, cursor_x+8, 44);
cursor_x -= 8;
boxfill8(sheet->buf, sheet->bxsize, COL8_000000, cursor_x,
28, cursor_x + 7, 43);
sheet_refresh(shtctl, sheet, cursor_x, 28, cursor_x+8, 44);
} else {
if (cursor_x < 240 &&i< 0x54 && keytable[i] != 0) {
boxfill8(sheet->buf, sheet->bxsize, COL8_000000, cursor_x,
28, cursor_x + 7, 43);
sheet_refresh(shtctl, sheet, cursor_x, 28, cursor_x+8, 44);
s[0] = keytable[i];
s[1] = 0;
showString(shtctl, sheet, cursor_x, 28, COL8_FFFFFF, s);
cursor_x += 8;
}
}
}
....
}
The value of variable i is the keyboard data passed in by CMain
If the value of i is 0x0e, it means that the keyboard button is the backspace key.
What we have to do when receiving this key is to delete the character in front of the cursor.
One thing we need to pay attention to when deleting characters is the handling of the cursor
Since the cursor effect is achieved by drawing a white square and then a black square
If the cursor happens to be a white square when backspace
Then when we move the cursor forward one character, a white square will be left at the original position of the cursor.
Therefore, before moving the cursor forward, we must first draw a square of the same size with the background color at the original position of the cursor.
The white square is covered. This is why cursor_x -= 8;
there are three statements for drawing the window before this statement.
After moving the cursor forward one character, the character in front of the cursor will be covered by the cursor square, thereby achieving the special effect of character deletion.
The same applies if other characters are entered.
First cover the current position of the cursor with the background color and then display the characters.
Then move the cursor back one character and draw the cursor square alternately at the new position.