I was always intrigued on how TSR programs worked so I decided to begin creating an extremely primitive one.
This C program reads keyboard keystrokes and prints them on console screen unless you type a
then b
then c
then a message box will tell you that you typed abc
and exits also q
will cause the program to quit.
Some notes, the program is taking a least 15% of my CPU in task manager, why?
I tried comparing the keystroke to 27
as in ESC but it didn't work.
#include <stdio.h>
#include <conio.h>
#include <Windows.h>
int main() {
char i;
while (1) {
if (kbhit()) {
i = getch();
if (i == 'q')
return 0;
if (i == 'a') {
i = getch();
if (i == 'b') {
i = getch();
if (i == 'c') {
int ans = MessageBox(NULL, "You typed abc\nPress OK to exit", "TSR confirmation", MB_ICONINFORMATION | MB_OK);
if (ans == 1)
return 0;
}
}
}
printf("You pressed %c\n", i);
}
}
return 0;
}
My questions: The console still shows, I tried to compile it as a Windows program, it loaded itself in memory and I lost it without the console and had to use Task Manager to kill it, is there a way to make it resident in memory and always monitor my keystrokes for the sequence without the console.
Is there a better way to listen to a longer sequence of characters without nested if
s, it becomes ugly with longer sequences.