Click here to Skip to main content
15,888,165 members
Articles / Desktop Programming / MFC
Article

Onscreen Keyboard

Rate me:
Please Sign up or sign in to vote.
4.71/5 (34 votes)
21 Mar 2000 666.6K   16.9K   102   92
An onscreen keyboard for pen computing and touchscreens
  • Download source files - 143 Kb

    Sample Image - OnscreenKeyboard.gif

    I needed a simple onscreen keyboard to interface with a touch / pen based computer that I bought through an online auction. Although Microsoft supplies one with its Pen Extensions 2.0 it only works win Win95 and I wanted to use NT.

    All of the available onscreen keyboards I found were either too expensive, or they were only designed to operate with 3.1 and 9x so (of course) I decided to see if I could make one. Along the way I learned a little about how the keyboard is handled, and the thread keyboard maps.

    Included with this article is a working, but simple, onscreen keyboard. I submitted it mostly as an example of the methods:

    • AttachThreadInput
    • keybd_event
    • VkKeyScan
    • GetKeyState

    This program has one nasty limitation, and that is that it "flashes" since it does not prevent the changing of focus to itself when clicked, and then back to the target window when the keystroke is generated. If someone wants to tackle that one please feel free ;-)

  • License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    QuestionRe: Tentative Enhancement Pin
    Homero De la Garza5-Aug-07 8:37
    Homero De la Garza5-Aug-07 8:37 
    GeneralRe: Tentative Enhancement Pin
    gidtitan21-Mar-07 10:07
    gidtitan21-Mar-07 10:07 
    QuestionJapanese? Pin
    matsnas6-Dec-05 22:07
    matsnas6-Dec-05 22:07 
    GeneralTimer Pin
    simcon7-Jun-05 23:20
    simcon7-Jun-05 23:20 
    Generalexecute Pin
    simcon9429-May-05 23:58
    simcon9429-May-05 23:58 
    GeneralKill keyboard from another app Pin
    Member 133159416-Feb-05 11:10
    Member 133159416-Feb-05 11:10 
    GeneralRe: Kill keyboard from another app Pin
    Dirk Moshage21-Feb-05 6:26
    Dirk Moshage21-Feb-05 6:26 
    GeneralRe: Kill keyboard from another app Pin
    Member 133159425-Feb-05 6:45
    Member 133159425-Feb-05 6:45 
    This doesn't deal exactly with the question I asked before, but I thought I would post it here in hopes it might help someone else since it held me up for 3 days. I needed a onscreen keyboard that could display 3 different keyboard types. A standard keyboard, a block keyboard, and a numbers only keypad. The block has all the letters in a row I.E. A B C. Its for people that have trouble typing. I wanted the keyboard to be a seperate app, not a control on each form. This way I could use it in other apps. I used the code here and another that is linked in to here in a different message. The other code has the dialogs generated in memory instead of being in a resource file, which helps with the number pad. I needed to assign a class name to the dialog instead of using the default #32770. I needed to do this so I could read the title on the dialog to know which keyboard was being displayed. In this way the user can train the keyboard for each form and the location and keyboard type are stored in a xml file. Assigning a class name was easy with the original code, put a problem with the code that generated the template in memory. I haven't done c++ in years, doing vb.net for the last couple. So it took some time to get back into doing c++. While I found many references to the DLGTEMPLATE I couldn't find clear examples where they assigned the class name. Most had the class name defaulting. The docs said the class name is a sz_or_ord data type. Never found what kind of data type this is. Below is the code assignes the class name.
    LPDLGTEMPLATE CKeyboardDialog::BuildDlgTemplate() const
    {
    USES_CONVERSION;

    struct MYDLGTEMPLATE
    {
    DLGTEMPLATE data;
    WORD wMenu;
    WORD szWndClass[17];
    wchar_t szCaption[1];
    };

    int len = sizeof(struct MYDLGTEMPLATE) + m_strCaption.GetLength()*sizeof(wchar_t);
    struct MYDLGTEMPLATE* dlgtemplate = (struct MYDLGTEMPLATE*)malloc(len);

    dlgtemplate->data. x = 0;
    dlgtemplate->data. y = 0;
    //dlgtemplate->data.cx = m_bNumeric ? 100 : 309;
    //dlgtemplate->data.cy = m_bNumeric ? 92 : 88;
    dlgtemplate->data.cx = m_bNumeric ? 60 : 278;
    dlgtemplate->data.cy = m_bNumeric ? 78 : 64;
    dlgtemplate->data.style = DS_SYSMODAL | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME;
    dlgtemplate->data.dwExtendedStyle = WS_EX_TOOLWINDOW | WS_EX_CLIENTEDGE | WS_EX_APPWINDOW;
    dlgtemplate->data.cdit = 0;
    dlgtemplate->wMenu = 0;
    MultiByteToWideChar(
    CP_ACP,
    0,
    m_strClassName,
    -1,
    dlgtemplate->szWndClass,
    sizeof(dlgtemplate->szWndClass) );
    wcscpy(dlgtemplate->szCaption, T2CW(m_strCaption));

    return &dlgtemplate->data;
    }
    The m_strClassName is OnScreenKBDClass
    The docs did keep talking about the fact the the class name needed to be a MultiByteToWideChar so the above might be a no brainer.
    Below is code the registor the class it comes from MSDN
    BOOL CApp::InitInstance()
    {

    WNDCLASS wc;

    // Get the info for this class.
    // #32770 is the default class name for dialogs boxes.
    ::GetClassInfo(AfxGetInstanceHandle(), "#32770", &wc);

    // Change the name of the class.
    wc.lpszClassName = "OnScreenKBDClass";

    // Register this class so that MFC can use it.
    AfxRegisterClass(&wc);
    Like I said I hope this helps some one else looking for an example.
    Thanks for the creat code.

    John


    John Barnes
    GeneralUsing the Windows OSK Pin
    Alex Evans29-Jan-05 18:35
    Alex Evans29-Jan-05 18:35 
    GeneralRe: Using the Windows OSK Pin
    EjayBoy1-Aug-06 12:04
    EjayBoy1-Aug-06 12:04 
    GeneralRe: Using the Windows OSK Pin
    EjayBoy1-Aug-06 13:10
    EjayBoy1-Aug-06 13:10 
    GeneralDon't work on remote desktop logon window Pin
    yoslee20-Jan-05 21:11
    yoslee20-Jan-05 21:11 
    GeneralOnscreen Keyboard Internationalization Matter ... Pin
    loretto11-Jan-05 2:04
    loretto11-Jan-05 2:04 
    Generalabt OnScreen Keyboard Pin
    Tripura.K3-Dec-04 0:06
    Tripura.K3-Dec-04 0:06 
    Generalabt OnScreen Keyboard Pin
    Anonymous2-Dec-04 22:31
    Anonymous2-Dec-04 22:31 
    GeneralCan't Compile Pin
    Member 15195161-Dec-04 23:57
    Member 15195161-Dec-04 23:57 
    GeneralRe: Can't Compile Pin
    User 10706397-Dec-04 1:47
    User 10706397-Dec-04 1:47 
    Generalhelp Pin
    davidorff16-Sep-04 22:37
    davidorff16-Sep-04 22:37 
    GeneralRe: help Pin
    Member 153990417-Apr-05 6:29
    Member 153990417-Apr-05 6:29 
    GeneralRe: help Pin
    cfo5ter5-Sep-06 12:24
    cfo5ter5-Sep-06 12:24 
    Generalenquiry Pin
    davidorff12-Sep-04 20:46
    davidorff12-Sep-04 20:46 
    GeneralRe: enquiry Pin
    Dirk Moshage14-Sep-04 5:20
    Dirk Moshage14-Sep-04 5:20 
    GeneralRe: enquiry Pin
    davidorff14-Sep-04 21:15
    davidorff14-Sep-04 21:15 
    GeneralI want to use on-screen keyboard for touch screen Pin
    misaaeshow29-Aug-04 23:56
    misaaeshow29-Aug-04 23:56 
    Generalvery slow when sending textt to MSN messenger text box Pin
    Anonymous11-Aug-04 19:59
    Anonymous11-Aug-04 19:59 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.