Solved

sending special charcters to buffer

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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
How to copy an image file into clipboard C/C++? 1 250
How to gracefully close the c++ 11 thread? 3 155
Error creating a new C++ project in ,net 20 59
print bytes of an integer 6 48
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 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 learn how to clear a vector as well as how to detect empty vectors in C++.

737 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