Solved

sending special charcters to buffer

Posted on 2004-10-20
4
380 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
ID: 12363855
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
ID: 12367049
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
ID: 12370467
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
ID: 12372490
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

867 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

15 Experts available now in Live!

Get 1:1 Help Now