Solved

sending special charcters to buffer

Posted on 2004-10-20
4
403 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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.
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.

691 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