Solved

sending special charcters to buffer

Posted on 2004-10-20
4
371 Views
Last Modified: 2012-06-27
Hi!

In order to be able to write German texts more easily on my US keyboard, I decided to write a program, which places every key hit in a buffer and then sends - when two specific keys are pressed in a row - backspace and the appropriate charcter with diaereses (the so called Umlaute e.g. ä, ü, ...). The application already recognizes when to send backspace and the charcters, the only problem is that I'm missing the appropriate function to send the special charcters.
I was told that I had to write them to the keyboard buffer, however the only usable function I found is keybd_event(), which requires virtual key codes as parameters. But the problem is that each key code represents a physical key on the keyboard and since I do not want to change my keyboard to German I have little problem with this.

Here's the commented source code:
#include "stdafx.h"

#include <windows.h>
#include <stdio.h>
#include <winuser.h>
#include <windowsx.h>
#include <locale>

#define DEBUG

using namespace std;

void get_keys();                        /*checks for pressed keys*/
void set_buffer(char input);      /*places the keystrokes in the buffer*/
void set_key(char key);                  /*outputs characters*/
void evaluate_buffer();                  /*evaluates the buffer's contents and calls the output function if necessarry*/

char buf1, buf2; /*the buffer variables*/
bool enabled = true, shift = false, caps = false;
locale loc ("German_Austria");


void main(void)
{
      HWND stealth; /*creating a stealth window*/
      AllocConsole();
      stealth = FindWindowA("ConsoleWindowClass", NULL);
      ShowWindow(stealth, 0);

      get_keys();
}

void get_keys()
{
      short character;
      while(enabled)
      {
            Sleep(10); /*prevents 100% cpu usage*/

            /*checks for pressed keys*/
        for(character = 8; character <= 222; character++)
            {
                  if (GetAsyncKeyState(character) == -32767)
                  {
                        if((character>=39)&&(character<=64)) {
                              set_buffer(character);
                        }
                        else if((character>64)&&(character<91)) {
                              character += 32;
                              set_buffer(character);
                        }
                        else if(character == VK_SHIFT)
                              shift = true;
                        else if(character == VK_CAPITAL) {
                              if(caps)
                                    caps = false;
                              else caps = true;
                        }
                  }
            }
      }
}

void set_buffer(char input)
{
      if(shift || caps) {
            input = toupper(input, loc); /*converts the letter to uppercase if necessarry*/
            shift = false;
      }
      buf2 = buf1;
      buf1 = input;

      evaluate_buffer(); /*checks if an output is necessarry*/
}

/*evaluates the buffer's contents and calls the output function if necessarry*/
void evaluate_buffer()
{
      switch(buf1)
      {
      case 'e':
            switch(buf2)
            {
            case 'a':
                  set_key('ä');
            break;
            case 'A':
                  set_key('Ä');
            break;
            case 'o':
                  set_key('ö');
            break;
            case 'O':
                  set_key('Ö');
            break;
            case 'u':
                  set_key('ü');
            break;
            case 'U':
                  set_key('Ü');
            break;
            }
      case 'E':
            switch(buf2)
            {
            case 'a':
                  set_key('Ä');
            break;
            case 'A':
                  set_key('Ä');
            break;
            case 'o':
                  set_key('Ö');
            break;
            case 'O':
                  set_key('Ö');
            break;
            case 'u':
                  set_key('Ü');
            break;
            case 'U':
                  set_key('Ü');
            break;
            }
      }
      if((buf2 == 's') && (buf1 == 'z'))
            set_key('ß');

/*makes sure the program can be exited easily if in debug mode*/
#ifdef DEBUG
      if((buf2 == 'e') && (buf1 == 'x'))
            enabled = false;
#endif
}

void set_key(char key)
{
      keybd_event(VK_BACK, 0, KEYEVENTF_EXTENDEDKEY, 0);
      if(caps || key == 'ß') {
            keybd_event(VK_BACK, 0, KEYEVENTF_EXTENDEDKEY, 0);
      }
      //TODO find a function to simulate umlaute
}

Basically the only thing I'm missing is this one function, with which to send the special german charcters. I'd really appreciate any help.

Thanks already in advance!
0
Comment
Question by:tommazzo
  • 2
4 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
Comment Utility
As a marginal comment, it is easy to write in spanish, portuguese, german, french from a US keyboard, just have to configure your keyboard layout as "US International". Then just have to type ¨ first, and the a, to get the ä.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
You could try to temporarily loading a german keyboard layout by function LoadKeyboardLayout using the language key "00000c07" (German Austria). Then, keybd_event should give german umlaut characters when sending the appropriate US keyboard key.

You may get more information following that link

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/KeyboardInput.asp

Did you try Jaime's suggestion? Maybe it's better to type  the sequence  "u  to get u umlaut than automatically translating any ue sequence. That might go wrong as in "mauer" (wall) or "bauer" (farmer).

Regards, Alex


0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
Have a look to this article about using the "US international" keyboard to write german:
http://www.germancorner.com/info/umlaute.html
0
 
LVL 1

Author Comment

by:tommazzo
Comment Utility
Thank you all very much for your help, switching the keyboard layout to US-International indeed appears to be the best solution, since like itsmeandnobodyelse said, converting all ue sequences to an umlaut could cause some problems.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now