Click here to Skip to main content
13,352,879 members (49,244 online)
Click here to Skip to main content
Add your own
alternative version


73 bookmarked
Posted 4 Apr 2005

XKeyboard - on-screen keyboard

, 4 Apr 2005
Rate this:
Please Sign up or sign in to vote.
XKeyboard eliminates the threat of keyloggers by allowing the user to enter a password by clicking on buttons displayed on an on-screen keyboard.
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->


The company where I work has a number of products where user is prompted for a password or other type of security code. The recent $420 million scam involving keylogger spooked everybody, so we started looking at alternative technologies to secure access to sensitive data. Although some of these technologies are quite expensive, it occurred to me that there was a cheap alternative to keyboard entry that would be simple to implement: an on-screen keyboard, where user enters a password by clicking on keys that are displayed on a virtual keyboard. The easiest way to explain is to show you what I mean:

Windows XP:

Windows 2000:

XKeyboard Features

XKeyboard offers following features:
  • All keyboard characters - The two Shift buttons are toggle buttons implemented using my CXButtonXP class. This allows access to all characters available on a real keyboard, including special characters. For ease of use, Shift keys on real keyboard also cause the XKeyboard keys to be shifted.
  • Show plain text - The user can display plain text of password:


  • Minimum/maximum password length - You can specify how long password must be.
  • Most recently used (MRU) passwords - You can specify a list of passwords that have been recently used. A password that is similar to any of the passwords in list will be rejected. The virtual function CheckMRUPasswords() allows you to customize algorithm used to determine similarity.
  • Dialog timeout - You can specify a timeout for dialog, which will force it to close after timeout expires. This will prevent possibility of leaving dialog open, with a password in edit control. In case of timeout, dialog returns code IDABORT.


  • Read-only edit control - You can specify that edit control should be read-only. If not read-only, user may enter a password by typing on real keyboard, which somewhat defeats the purpose.
  • Upper/lower case - You can specify to convert password to upper or lower case, or leave it unchanged.
  • Special characters - You can specify that password should include special characters, digits, or just alphanumeric characters. The virtual function CheckSpecial() allows you to customize algorithm used to determine whether password contains special characters.
  • String resources - all messages displayed by XKeyboard are contained in string resources.
  • Spacebar - You can specify to display spacebar. Not displaying spacebar means that spaces will not be allowed in password (even if edit control is not read-only). When spacebar is not displayed, the dialog is resized:


CXKeyboard Implementation Notes

As you can see from code, there is nothing special about XKeyboard. It is implemented as a dialog with buttons for usual keyboard keys. Pressing either Shift key toggles upper/lower case (and in this case, also toggles the special characters to "shifted" character).

One decision I made was to not use a checkbox button (in "push-like" mode). I didn't like the way button behaved on XP with theming - when you push it, it immediately pops back to display a non-pushed state, until you move the cursor off button, when it displays a pushed state. So I used my new XButtonXP control instead.

XKeyboard uses two timers; one timer is used if there is timeout set, and the other timer is used to check if shift keys on real keyboard are pressed. Pressing either real shift key works just as you would expect - as long as real shift key is held down, XKeyboard shows shifted character set. When real shift key is released, XKeyboard shows unshifted character set.

CXKeyboard API

The CXKeyboard API includes:

GetPassword() Returns the password
SetPassword() Sets the edit control with password
GetShowText() Returns state of Show Text checkbox
SetShowText() Sets state of Show Text checkbox
GetShowSpacebar() Returns TRUE if spacebar is visible
SetShowSpacebar() Sets visibility of spacebar (TRUE = visible)
GetSpecial() Returns enum value of special character setting
SetSpecial() Sets special character enum value
GetMinLength() Returns minimum length of a password
SetMinLength() Sets minimum length of password
GetMaxLength() Returns minimum length of a password
SetMaxLength() Sets minimum length of password
GetCase() Returns enum value of case setting
SetCase() Sets case enum value
SetMRUPasswords() Sets MRU password list
ClearMRUPasswords() Clears MRU password list
GetTimeout() Returns timeout value in seconds
SetTimeout() Sets timeout value in seconds; 0 = no timeout
GetReadOnly() Returns read-only state of edit control
SetReadOnly() Sets read-only state of edit control (TRUE = read-only)
CheckSpecial() Virtual function that returns TRUE if password contains a special character
CheckMRUPasswords() Virtual function that returns TRUE if password is in MRU list

How to use

To integrate CXKeyboard into your app, you first need to add following files to your project:

  • XKeyboard.cpp
  • XKeyboard.h
  • XKeyboardRes.h
  • XKeyboard.rc
  • XButtonXP.cpp
  • XButtonXP.h
  • XThemeHelper.cpp
  • XThemeHelper.h
  • OddButton.cpp
  • OddButton.h
  • MemDC.h

You also need to add XKeyboard.rc to your project's rc file - go to View | Resource Includes... and in bottom listbox, scroll down to end. Insert #include "XKeyboard.rc" right before #endif:


Next, include header file XKeyboard.h in appropriate project files. Now you are ready to start using CXKeyboard.

Compiler Prerequisites

XKeyboard requires the Platform SDK to compile. If you get a compile error about DFCS_HOT being undefined, you should include the following line in stdafx.h, before any header file includes:
#define WINVER 0x0500      // needed by XButtonXP

Calling XKeyboard

The demo app shows how to call XKeyboard:
// array of "most recently used" passwords
CStringArray sa;

CPoint point;

CXKeyboard dlg(point.x, point.y-247);
int n = _ttoi(m_strMinLength);
if (n <= 0)
    n = 1;

int rc = dlg.DoModal();
if (rc == IDOK)
    m_strPassword = dlg.GetPassword();
    m_bShowText = dlg.GetShowText();
else if (rc == IDABORT)
    GetDlgItem(IDC_STATUS)->SetWindowText(_T("Dialog timed out"));
    GetDlgItem(IDC_STATUS)->SetWindowText(_T("Dialog cancelled by user"));


Other On-Screen Keyboard Implementations

There is a Microsoft on-screen keyboard (Programs | Accessories | Accessibility) that is shipped with Win2k and XP. Microsoft licensed this product from Madentec Limited.

Click-N-Type is a freeware on-screen keyboard that is very popular.

Both of these products are available only as standalone executables, although the Madentec site mentions that an SDK is available.

Revision History

Version 1.0 - 2005 April 4

  • Initial public release


This software is released into the public domain. You are free to use it in any way you like, except that you may not sell this source code. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Hans Dietrich
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.

Recently, I have moved to Los Angeles where I am doing consulting and development work.

For consulting and custom software development, please see

You may also be interested in...

Comments and Discussions

QuestionImages No Being Displayed Pin
Rick York19-Jun-12 13:30
memberRick York19-Jun-12 13:30 
GeneralMy vote of 5 Pin
Subhajeet Ganguly24-Oct-11 1:04
memberSubhajeet Ganguly24-Oct-11 1:04 
Questiona simple question Pin
danceoften14-Dec-09 4:14
memberdanceoften14-Dec-09 4:14 
GeneralNot an Adoptable Solution [modified] Pin
aspdotnetdev28-Aug-09 13:51
memberaspdotnetdev28-Aug-09 13:51 
QuestionNum Lock, Caps Lock, Scroll Lock Pin
Touseefliaqat29-Aug-06 5:59
memberTouseefliaqat29-Aug-06 5:59 
AnswerRe: Num Lock, Caps Lock, Scroll Lock [modified] Pin
Brad_Jones8-Sep-06 7:38
memberBrad_Jones8-Sep-06 7:38 
Questioncan not find uxtheme.h Pin
narutox227-Jun-05 4:36
membernarutox227-Jun-05 4:36 
AnswerRe: can not find uxtheme.h Pin
jamesw200315-Jun-05 13:08
memberjamesw200315-Jun-05 13:08 
AnswerRe: can not find uxtheme.h Pin
cristitomi19-Mar-07 1:00
membercristitomi19-Mar-07 1:00 
GeneralForeign Language Support Pin
Alex Evans12-Apr-05 15:05
memberAlex Evans12-Apr-05 15:05 
GeneralWindows XP osk Pin
Anonymous12-Apr-05 10:00
sussAnonymous12-Apr-05 10:00 
GeneralRe: Windows XP osk Pin
tsurutsuru14-Apr-05 11:38
membertsurutsuru14-Apr-05 11:38 
GeneralRe: Windows XP osk Pin
RancidCrabtree15-Apr-05 7:05
memberRancidCrabtree15-Apr-05 7:05 
GeneralRe: Windows XP osk Pin
vincennes25-Sep-08 5:16
membervincennes25-Sep-08 5:16 
General.NET Pin
Uwe Keim4-Apr-05 9:43
sitebuilderUwe Keim4-Apr-05 9:43 
GeneralRe: .NET Pin
Hans Dietrich4-Apr-05 11:13
memberHans Dietrich4-Apr-05 11:13 
GeneralRe: .NET Pin
WREY4-Apr-05 14:02
memberWREY4-Apr-05 14:02 
GeneralRe: .NET Pin
Uwe Keim4-Apr-05 17:28
sitebuilderUwe Keim4-Apr-05 17:28 
GeneralIs this safe?! Pin
Geert van Horrik4-Apr-05 8:13
memberGeert van Horrik4-Apr-05 8:13 
GeneralRe: Is this safe?! Pin
Hans Dietrich4-Apr-05 8:23
memberHans Dietrich4-Apr-05 8:23 
GeneralMessage Closed Pin
4-Apr-05 15:08
memberCap'n Code4-Apr-05 15:08 
GeneralRe: Is this safe?! Pin
Manasjyoti Sharma30-Oct-07 1:18
memberManasjyoti Sharma30-Oct-07 1:18 
GeneralA password alternative Pin
Pecuchet4-Apr-05 7:14
memberPecuchet4-Apr-05 7:14 
GeneralRe: A password alternative Pin
Hans Dietrich4-Apr-05 7:26
memberHans Dietrich4-Apr-05 7:26 
GeneralRe: A password alternative Pin
Pecuchet4-Apr-05 8:07
memberPecuchet4-Apr-05 8:07 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 4 Apr 2005
Article Copyright 2005 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid