Solved

sending special charcters to buffer

Posted on 2004-10-20
4
391 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best book to learn C++ 4 84
Header of docx file 17 117
How do I save this device context as a .bmp or a picture of some kind? 2 64
learn programming 8 42
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

830 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