Emulate an '@' keypress or any other char

This is a discussion on Emulate an '@' keypress or any other char within the Windows Programming forums, part of the Platform Specific Boards category; I know how I can use WM_KEYDOWN and WM_KEYUP to emulate keypresses or keyboard_event but this only seems to work ...

  1. #1
    Registered User Kirdra's Avatar
    Join Date
    Aug 2002
    Posts
    105

    Emulate an '@' keypress or any other char

    I know how I can use WM_KEYDOWN and WM_KEYUP to emulate keypresses or keyboard_event but this only seems to work for virtual key codes. How do I emulate character keys like '@'.

    Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Number keys are their ASCII values. Letter keys are the ASCII values of the capital letter involved. @ would, I would guess, involve shift+2.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,820
    Quote Originally Posted by tabstop View Post
    @ would, I would guess, involve shift+2.
    I doubt that, because that is only the layout of the US keyboard.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    on windows there is application Character Set that gives a possibility to exemine the codes of different characters in different encoding tables
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Quote Originally Posted by Kirdra View Post
    I know how I can use WM_KEYDOWN and WM_KEYUP to emulate keypresses or keyboard_event but this only seems to work for virtual key codes. How do I emulate character keys like '@'.

    Thanks.
    Have you tried using VK_MULTIPLY which I believe might be the @ sign?

  6. #6
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    611
    VkKeyScan will tell you the VK for a specified character. Here's a quick demo of it since MSDN doesn't seem to have one which shows where the at key is on standard UK, US, and Dutch keyboards.

    Code:
    #include <iostream>
    #include <cstdio>
    #include <windows.h>
    
    void PrintKeyInfo(WCHAR key, USHORT vk, USHORT shift, LPCWSTR layout)
    {
        // figure out the scancode for the key
        BYTE scanCode = static_cast<BYTE>(MapVirtualKeyW(vk, MAPVK_VK_TO_VSC));
        // put it in the bit positions specified by the docs
        LONG keyParam = scanCode << 16;
        // display data
        WCHAR keyName[10] = {0};
        GetKeyNameTextW(keyParam, keyName, ARRAYSIZE(keyName));
        std::wcout << L"In keyboard layout "
            << layout << L", '" << key << L"' is at VK 0x"
            << std::hex << vk << L" (" << keyName << L") "
            L"and you ";
        if(!shift)
        {
            std::wcout << L"don't ";
        }
        std::wcout << L"need to hold shift to get at it\n";
    }
    
    void PrintKeyInfoForLang(WCHAR key, LANGID lang)
    {
        // save the current key layout
        HKL currentKeyLayout = GetKeyboardLayout(GetCurrentThreadId());
        WCHAR layoutName[10];
        // create the layout name
        swprintf(layoutName, L"0000%04x", lang);
        // load the layout
        HKL keyLayout = LoadKeyboardLayoutW(layoutName, KLF_ACTIVATE | KLF_NOTELLSHELL);
        if(keyLayout)
        {
            // get key details
            SHORT keyDetails = VkKeyScanExW(key, keyLayout);
            USHORT vkValue = LOBYTE(keyDetails); // the Virtual Key 
            USHORT shiftState = HIBYTE(keyDetails); // the shift state
            PrintKeyInfo(key, vkValue, shiftState, layoutName);
            ActivateKeyboardLayout(currentKeyLayout, 0);
            UnloadKeyboardLayout(keyLayout);
        }
        else
        {
            std::cout << "Couldn't load keyboard layout\n";
        }
    }
    
    int main()
    {
        WCHAR at = WCHAR(0x40);  // 0x40 is @ in UTF-8/UTF-16
        
        PrintKeyInfoForLang(at, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_UK));
        PrintKeyInfoForLang(at, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));
        PrintKeyInfoForLang(at, MAKELANGID(LANG_DUTCH, SUBLANG_DUTCH));
    
        return 0;
    }
    Which gives me
    In keyboard layout 00000809, '@' is at VK 0xc0 (') and you need to hold shift to get at it
    In keyboard layout 00000409, '@' is at VK 0x32 (2) and you need to hold shift to get at it
    In keyboard layout 00000413, '@' is at VK 0xde (@) and you don't need to hold shift to get at it
    Last edited by adeyblue; 02-03-2009 at 08:36 PM.

  7. #7
    Registered User Kirdra's Avatar
    Join Date
    Aug 2002
    Posts
    105
    Thank you adeyblue, that looks very helpful

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  4. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  5. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21