Click here to Skip to main content
Email Password   helpLost your password?

Sample Image - touchscreenkeyboard.jpg

Introduction

Long story short....

For a touch-screen enabled application for a local museum, I needed an onscreen keyboard for visitors to enter comments, search criteria, and whatever else may crop up during the design. After searching online, I found plenty of keyboards, but none that fit the bill. Most were just too small for use with fingers, or were otherwise unsuitable. I do not want users to be able to hit the Windows key, or use function keys, CTRL keys, etc. I did find one that almost fit the bill - I could design my own layout with keys of my choice, it was sizeable, it remained on top, it was almost perfect. The problem was they wanted money for it! Well, that and a few other minor but workable details.

The solution: make my own. Utilizing my vast experience with C# (or any other version of C) totaling nearly 3 weeks now, I set out to make my own user control. The first step was to learn what it was and how to make one. The second step was to do it.

To get the demo to compile correctly, first open and build the source files, then open the demo file and delete TouchscreenKeyboard from the References file in the Solution Explorer, then re-add it with Add Reference / Browse / {add TouchscreenKeyboard.dll from the directory you saved the SRC file to}.

Creating the Keyboard

The first step was to make a keyboard which, for this example, is just a JPG image I made using Paint - I tried to photograph my own keyboard, but because of the flash-glare, I couldn't get a workable picture to restructure. I decided to make three different types of keyboards, one with a standard QWERTY layout, one where I reordered the letter keys alphabetically, and the third for younger kids. The keyboard can be set both at design time and changed programmatically without affecting any existing user-typed text. The user control consists of just four PictureBoxes, one holding the selected keyboard, and three "empty" ones used to simulate the Shift and Caps Lock keys depressed.

Note that for the kids keyboard, the entire keyboard is replaced (one has lowercase and the other has uppercase letters), and the three PictureBoxes are hidden.

Sample Image - standardkeyboard.jpg

Sample Image - alphabeticalkeyboard.jpg

Sample Image - kidsupperkeyboard.jpg

Sample Image - kidslowerkeyboard.jpg

The next step was to layout the grid for determining which key was pressed. I simply divided the keyboard into two sections, the main key section, and the cursor movement section, then split each into rows, and columns for each row, resulting in a (x,y) range for each key. The standard and alphabetical keyboards share the same grid, and I use Swith Case code to swap letters if required. The kids keyboard required its own grid. About halfway through, I realized that I need to account for resizing the control, so hard-coding (x,y) coordinates and using the mouse-click coordinates would not work. The simple solution: all locations are merely a ratio of the total length and width of the keyboard. Obviously, it is easier to adjust the mouse-click position than to adjust the position of each key, so using the original size of the keyboard, a resized (x,y) mouse-click coordinate would be determined as shown in the control's MouseClick event:

private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
     Single xpos = e.X;   // mouse X coordinate

     Single ypos = e.Y;   // mouse Y coordinate


     xpos = 993 * (xpos / pictureBox1.Width);  // 993 = fullsize width

     ypos = 282 * (ypos / pictureBox1.Height); // 282 = fullsize height


     mstrKeyPressed = HandleTheMouseClick(xpos, ypos);
     KeyboardEventArgs dea = new KeyboardEventArgs(mstrKeyPressed);
     OnKeyThrown(dea);
}

The same methodology is used to determine the (x,y) coordinates for locating the Shift and Caps Lock PictureBoxes, and their new sizes. This is done within the controls' SizeChanged event.

private void pictureBoxKeyboard_SizeChanged(object sender, EventArgs e)
{
     // position the capslock and shift down overlays


     pictureBoxCapsLockDown.Left = 
       Convert.ToInt16(pictureBoxKeyboard.Width * 5 / 993);
     pictureBoxCapsLockDown.Top = 
       Convert.ToInt16(pictureBoxKeyboard.Height * 115 / 282);
     pictureBoxLeftShiftDown.Left = 
       Convert.ToInt16(pictureBoxKeyboard.Width * 5 / 993);
     pictureBoxLeftShiftDown.Top = 
       Convert.ToInt16(pictureBoxKeyboard.Height * 169 / 282);
     pictureBoxRightShiftDown.Left = 
       Convert.ToInt16(pictureBoxKeyboard.Width * 681 / 993);
     pictureBoxRightShiftDown.Top = pictureBoxLeftShiftDown.Top;


     // size the capslock and shift down overlays


     pictureBoxCapsLockDown.Width = 
       Convert.ToInt16(pictureBoxKeyboard.Width * 110 / 993);
     pictureBoxCapsLockDown.Height = 
       Convert.ToInt16(pictureBoxKeyboard.Height * 55 / 282);
     pictureBoxLeftShiftDown.Width = 
       Convert.ToInt16(pictureBoxKeyboard.Width * 136 / 993);
     pictureBoxLeftShiftDown.Height = 
       Convert.ToInt16(pictureBoxKeyboard.Height * 55 / 282);
     pictureBoxRightShiftDown.Width = 
       Convert.ToInt16(pictureBoxKeyboard.Width * 135 / 993);
     pictureBoxRightShiftDown.Height = pictureBoxLeftShiftDown.Height;
}

For the standard and alphabetical keyboards, I wanted them to behave like regular keyboards, that is Caps Lock on will only return capitalized letters, and not ! instead of 1. This is handled by the functions HandleShiftableKey(string theKey) and HandleShiftableCaplockableKey(string theKey). For this control, Shift remains active for one key-press, and Caps Lock remains on until clicked again.

As shown in the demo project, the value of the key pressed is returned via the control's UserKeyPressed method.

private void keyboardcontrol1_UserKeyPressed(object sender, 
             KeyboardClassLibrary.KeyboardEventArgs e)

// this is demo project code showing

// how the control can be used

{
     // ensures focus is on the textbox control

     richTextBox1.Focus();
     // Sendkeys.Send(key) acts as though

     // a standard keyboard key was pressed.

     SendKeys.Send(e.KeyboardKeyPressed);
}

That's pretty much it. The source file contains everything for creating the control, and the demo project contains everything for the sample application pictured above. There isn't a whole lot of code, so tracing it to see how it works (or controls in general) should be easy. This is my first control and the first article I've ever written, so I'd appreciate any comments.

Update History

April 06, 2006

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralTrying to access a textfield on another form
Phil30
23:03 20 Jan '10  
Thank you FoxholeWilly for the excellent work and good explanations. It helped me a lot to realise my project. But its still not optimal. Having the Keyboard on the the main form is not ideal because the main form is cluttered with textfields and I want to use the keyboard for other forms as well. Does anybody know how I can access textfields on other forms from a seperate keyboard form where the virtual keyboard is.
Ive been googling so far without any success. I would appreciate it if somebody could pass me a link or explain me how to reach my goal.
Greets Phil
GeneralRe: Trying to access a textfield on another form
Genesis Technology
10:48 18 Feb '10  
I'm not sure if you've solved this yet or not, but we've used our own virtual keyboard and trapped key events back on to other forms.
GeneralRe: Trying to access a textfield on another form
Phil30
23:01 1 Mar '10  
Thank you for your reply. I have not solved the problem, but I think I would be able to solve it now. When I have time I might work on it again.
GeneralWPF ?
fpscomponents
2:16 11 Jan '10  
great, as for WPF you can check flexible keyboard: FPS Input Controls[^]. the advantage of it: custom styles/themes, layout manager to create any layout and support any language you can add to your Windows system and tune with layout manager
Generala simple question
danceoften
4:19 14 Dec '09  
thanx very much for this component.
i would like to iterate all keys in the currently attached(configured) keyboard, but all solutions i found on this site and on the internet use fixed layouts or a range of fixed layouts
i would like to know wich keys-keycodes are available on the current locale keyboard.
could you adress me to some point ?

thank you

DanceOften
www.danceoften.com

GeneralTab key
PQSIK
8:33 12 Sep '09  
Hi there,

I was wondering if it would be easy to add a tab key and set the focus to controls.

If so where would I add the code for the tab?

Thanks

PQSIK
GeneralRe: Tab key -- Answered
PQSIK
15:50 12 Sep '09  
Its ok, I worked it out thanks
GeneralAwesome Work! A real usable piece of work!
gilbert_leon
18:26 4 Sep '09  
I was like you, looking for some solution for schools where children and teenagers can access a touchscreen and identify themself. I found your solution and was impressed at the very first moment. I used and can't be thankful enough for such a good applications. Thanks again, I hope that someday I would have such a good idea like this and will post on the internet for everyone to benefit from it...
Regards
Gilbert
Generalplz help me out
harmanpreethind
3:40 21 Aug '09  
hello 2 all i m doing btech in electronic n communication... i m final year student i have to made project... i wanna make project on touch screen..keyboard can any one help abt dis project
GeneralGood Control
aries7811
21:47 3 Aug '09  
Hi FoholeWilly your control is to good to be use on my apps...Thumbs Up Thumbs Up Thumbs Up Thumbs Up
GeneralNice work!! but can you help me a bit?
daffy_2003
18:29 8 Jul '09  
Hi.
I wanna implement your keyboard to my application whereby the user need to double click the textbox then the keyboard appears.
How to go about in doing it?
please help and thanks!!
Generalcode project typingmaster
boy_vipvip
7:39 3 May '09  
I have this project typingmaster, who can share for me? thanks...
QuestionIs there any possiblities to set both Mouse Mode and touch screen mode?
Khathar
18:58 24 Feb '09  
dear all,

i need to know if we use touch screen then we cant use mouse mode?

if we cant use like that means is there any other provision to swap these functionalities?

and also i need to know is it possible to develop the MFC Project in VC6.0 which supports Touch Screen?

reply pls

thanks in advance

AK.
AnswerRe: Is there any possiblities to set both Mouse Mode and touch screen mode?
workingOnZen
9:14 26 Apr '09  
In Windows, this is handled by the OS and the drivers for you touch screen controller and your mouse.
QuestionCan we create a touch screen Project in VC 6.0 ?
Khathar
19:45 23 Feb '09  
Dear all,

im using vc 6.0 i need to know that is it possible to create an application in MFCAppWizard.exe to operate touch screen?

please let me know.

thanks in advance

yours...

Abdul
GeneralSLOVENIAN KEYBOARD LAYOUT
mmf
2:35 28 Jan '09  
Hi.

Any idea where to get image with Slovenian keyboard layout?

Tnx,
Matjaž
GeneralAutoScaleMode causes problems on non-standard font sizes.
Member 2053006
5:40 18 Nov '08  
An excellent keyboard control, thank you for submitting it.

One slight problem I found with it.

If you place it on a form with non-standard fonts then the control will grow every time the application is compiled. Changing AutoScaleMode to None solved that problem.
GeneralPassword Field problem
TheFrnd
4:33 25 Oct '08  
If I try to Send key to textbox that has a passwordChar set to '*'. I don know why I don get the keys to send to textbox-they are missed sometimes. Can anybody tell me what's the reason ?!

TheStranger

GeneralDoes not seem to work correctly with combo boxes
Member 1316913
9:41 14 Oct '08  
With combo boxes the cursor does not advance as it does using a real keyboard. Can anyone suggest a fix for this?
NewsMade a Touchscreen Numpad based on your Keyboard
Eqquituz
9:28 27 Jul '08  
After finally been able to use your keyboard I found it very usefull on my touchscreen application but I had a situation with it. When I just needed numbers the keyboard was a bit too big so I was forced to make a Numpad with the same style as your keyboard. I used all your code and images.

I can't publish the Numpad until the author of the keyboard allows me to.

Please contact me...
GeneralRe: Made a Touchscreen Numpad based on your Keyboard
FoxholeWilly
5:04 28 Jul '08  
Email sent granting permission.
GeneralRe: Made a Touchscreen Numpad based on your Keyboard
chrisclarke11
4:07 23 Jun '09  
Thanks for a great Keyboard and artical.

Do you have the images for the number keypad

Thanks

Chris

GeneralRe: Made a Touchscreen Numpad based on your Keyboard
wunchun
7:48 2 Aug '09  
Hi, I saw your posting about numeric keypad in C#, can you send me your code?

I really appreciate your sharing of code.

Thanks

Wunchun
GeneralRe: Made a Touchscreen Numpad based on your Keyboard
Tidusin
1:18 4 Feb '10  
Hello,

I'm also interested in your touchscreen numpad, i would like if you send the code also to me.

Regard's
GeneralRe: Made a Touchscreen Numpad based on your Keyboard
that4sale
6:27 9 Feb '10  
Hi, I am just doing a similar project where I need a Numpad on Touchscreen. Can you please send me a copy of code and pictures.

thanks,


Last Updated 6 Apr 2006 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010