Simple Text

<< Back to the SDG Toolkit page

In this tutorial you will learn how to handle multiple keyboard events.

Unzip all files (executables plus dependancies) into a single folder


Below is the full code required to make a minimal SDG Text program that works for two people with two attached mice and keyboards. When a person clicks on a spot, text typed from that person's keyboard will appear on that spot. Thus multiple users can enter text with multiple keyboards at multiple places simultaneously.

Some things you should know ahead of time.

  • Every keyboard has an ID, starting from 0. You can use this ID just as you use the ID for multiple Mice.
  • Usually, keyboards and mice with the same ID are associated with each other. It makes programming much easier!


1) Create the empty project. As in the other tutorials, create a C# project (Windows Application). Call it SimpleText.

2) Add SdgManager to the Form. Sdg Manager Component in the Toolbox in the Tab where you had previously installed it. Click on the SdgManager component and drag an instance onto the form. In Properties (bottom right), change RelativeTo to Form1.

3) Add Inherited User Control. In the Solution Explorer, right click SimpleText->Add->Add Inherited Control. Highlight Inherited User Control and name it TextPlaceWidget. Click open, then Browse. Find the location where you saved the SDG Toolkit and double click the dll. Highlight the dll and then click OK. The next few steps will involve programming this User Control rather than the Form.

4) Add keyboard event and mouse event. In the Properties of TextPlaceWidget, double click SdgKeyDown. Select the Design tab of TextPlaceWidget again, and double click SdgMouseDown.

5) Add two text buffers, one for each person. Add the following code after the line "private System.ComponentModel.IContainer components = null;"

private PointF[] TextEntryPoints = new PointF[2];
private string[] TextBuffer = new string[2];

6) Add code for key down event. Note that the text is associated with each key ID.

if (e.KeyCode == Keys.Enter) {
  TextBuffer[e.ID] = string.Empty;
else if (e.KeyCode != Keys.ShiftKey) {
  TextBuffer[e.ID] = TextBuffer[e.ID] + (char)e.KeyValue;

Font f = new Font("Times", 12, FontStyle.Italic);
Color c = Color.Red;

if (e.ID > 0) {
  f = new Font("Arial", 14, FontStyle.Bold);
  c = Color.Blue;

Graphics g = this.CreateGraphics();
g.DrawString(TextBuffer[e.ID], f, new SolidBrush(c), TextEntryPoints[e.ID]);

7) Add code for mouse down event. Note that what happens is specific to each mouse ID.

TextEntryPoints[e.ID] = new PointF((float) e.X, (float) e.Y);
Graphics g = this.CreateGraphics();

if (e.ID > 0)
  g.DrawEllipse(Pens.Blue, e.X, e.Y, 2, 2);
  g.DrawEllipse(Pens.Red, e.X, e.Y, 2, 2);
TextBuffer[e.ID] = string.Empty;

8) Build Solution. Otherwise, TextPlaceWidget cannot be used. Hit Ctrl+Shift+b to build.

9) Add TextPlaceWidget to Form1. Switch to the Form1 Designer. In the Toolbox under My User Controls, drag the TextPlaceWidget control to Form1 in the Design tab.

10) Resize TextPlaceWidget. In the Properties of TextPlaceWidget, change Dock to fill by clicking the middle square.

11) Compile.