Coding Education

How to change keyboard strokes (C/C++) Part 1/2

In the following simple C program, I’ll demonstrate how to capture keystrokes and turn them into something completely different…other than what the key labels read. This is the basic idea behind changing keyboards from English to another language; take the input from the key, read its character code (ordinal value) from the operating system, and change it to another code also stored in the operating system’s character tables, then display the changed character on display and editor.

This is also a way some of the malware programs exploit this feature to confuse the user, and even extort ransom money.

I’ll share with you 2 versions of the program in two different blogs.  Note that the programs are for education purposes only.  Do NOT use this for any malicious purpose! These programs will NOT permanently change your input system. They’ll only affect the console WHILE the programs are running and everything will return to normal after you quit them.

So, let’s get down to code!

Code and Explanation:

I’ll use getch() function that prompts the user (and waits) for an input…but only for 1 key press (not function keys), and store it in a variable c. Great! But I’ll display something else! Other than what was entered. I do that by selecting a character from an array s[] in sequence. And using good trusty printf() function, we display a new character from the s[] array instead of what was entered. With me so far?

char s[]="YOUR KEYBOARD IS BROKEN! YOU MUST CALL ME @ 555-555-5555 TO FIX IT!";

c=getch();

printf("%c\a",s[i]);

Good! So, that works one time. But I want it to go on for a while 🙂 So I use…guessed it! while() loop.

So, let’s wrap it in while(c) loop. Which says which there’s a key being pressed by the user, keep showing them another character (in sequence) from the s[] array that I concocted (above).

while(c)
{
c=getch();
printf("%c\a",s[i]);
i++;

}

s[i] of course is picking up the character from string array s using index variable i, which in C/C++ starts from 0. So, first one will be ‘Y’ and then ‘O’, and then ‘U’ and so on, and it’ll repeat as long as you play.

One more thing I did was just for readability…which is to go to next line after the whole sentence/string from s[] is used up. How? If index i has reached the string length, then just do a line-break. This is done by this statement:

if(i==strlen(s))
{
printf("\r\n"); i=0; // line break after each line in s[]
}

But where to put this statement? Within the while() loop. So core loop now is:

while(c)
{
c=getch();
printf("%c\a",s[i]);
i++;

if(i==strlen(s))
{
printf("\r\n"); i=0; // line break after each line in s[]
}
}

That’s good. Now of course, you need to wrap all these into a main() function as with any console C/C++ app, and include the right libraries such as:

<stdio.h>, <conio.h>, <string.h>

and initialize the variables accordingly which I did as this (inside main()):

char s[]="YOUR KEYBOARD IS BROKEN! YOU MUST CALL ME @ 555-555-5555 TO FIX IT!";
char c=1;
int i=0;

Boom! Now, let’s run this. You can download the app from here. It’s a zip file for security reasons. Once you open the zip and extract the .exe file, just double-click and have fun 🙂

I’ll share the next version in the next blog.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top