Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Using Raw Input from C# to handle multiple keyboards

, , 8 Mar 2013 CPOL
Windows XP supports multiple keyboards, but by default, the .Net Framework will treat them all as one. This article explains how to use the Windows API Raw Input methods to support multiple keyboards from a C# application.
Winfx RawInput
<Window x:Class="WindowsApplication1.Window1"
    Title="Raw Keyboard Input" Height="371" Width="736">
    <TextBox Height="22" Margin="158,62,144,0" Name="tbText" VerticalAlignment="Top" ></TextBox>
    <GroupBox Header="Latest Keystroke" Margin="20.3033333333333,97.04,21,0" Name="groupBox1" Height="65.96" VerticalAlignment="Top"></GroupBox>
    <GroupBox Header="Device Details" Margin="20.3033333333333,0,22,48" Name="groupBox2" Height="117.96" VerticalAlignment="Bottom"></GroupBox>
    <Label HorizontalAlignment="Left" Margin="35.0066666666667,121.04,0,0" Name="label3" Width="43.9933333333333" Height="27.96" VerticalAlignment="Top">Code:</Label>
    <Label HorizontalAlignment="Left" Margin="276.006666666667,122.04,0,0" Name="label4" Width="69.9933333333333" Height="27.96" VerticalAlignment="Top">Virtual Key:</Label>
    <Label Height="29.96" Margin="344.006666666667,119.04,55,0" Name="lbVKey" VerticalAlignment="Top" FontSize="16" Foreground="#FF0000FF"></Label>
    <Label HorizontalAlignment="Left" Margin="77.0066666666667,118.04,0,0" Name="lbKey" Width="171.993333333333" Height="27.96" VerticalAlignment="Top" FontSize="16" Foreground="#FF0000FF"></Label>
    <Label HorizontalAlignment="Left" Margin="38.0066666666667,0,0,120" Name="label5" Width="51.9933333333333" Height="25.96" VerticalAlignment="Bottom">Handle:</Label>
    <Label Margin="89.0066666666667,0,0,120" Name="lbHandle" HorizontalAlignment="Left" Width="79.9933333333352" Height="27.96" VerticalAlignment="Bottom" Foreground="#FF0000FF" FontSize="16"></Label>
    <Label Margin="204.006666666667,0,0,115" Name="label6" HorizontalAlignment="Left" Width="48.993333333333" Height="29.96" VerticalAlignment="Bottom">Type:</Label>
    <Label Margin="243.006666666667,0,0,120" Name="lbType" HorizontalAlignment="Left" Width="96.993333333333" Height="26.96" VerticalAlignment="Bottom" Foreground="#FF0000FF" FontSize="16"></Label>
    <Label Height="25.96" HorizontalAlignment="Right" Margin="0,0,147,120.02" Name="label7" VerticalAlignment="Bottom" Width="78.9933333333333"># Keyboards:</Label>
    <Label HorizontalAlignment="Right" Margin="0,0,86.9999999999998,122.04" Name="lbNumKeyboards" Width="60.9933333333333" Height="25.96" VerticalAlignment="Bottom" Foreground="#FF0000FF" FontSize="16"></Label>
    <Label Height="25.96" HorizontalAlignment="Left" Margin="37.0066666666667,0,0,88.04" Name="label8" VerticalAlignment="Bottom" Width="46.9933333333333">Name:</Label>
    <Label Height="30.96" Margin="86.006666666667,0,36,85.02" Name="lbName" VerticalAlignment="Bottom" Foreground="#FF0000FF" FontSize="16"></Label>
    <Label Height="25.96" HorizontalAlignment="Left" Margin="35.0066666666667,0,0,56.52" Name="label9" VerticalAlignment="Bottom" Width="80.9933333333333">Device Desc:</Label>
    <Label Height="28.96" Margin="112.006666666667,0,59,56.04" Name="lbDescription" VerticalAlignment="Bottom" Foreground="#FF0000FF" FontSize="16"></Label>
    <Button Height="23" Margin="221,0,259,14" Name="button1" VerticalAlignment="Bottom" Click="CloseMe" CommandTarget="{x:Null}">Close</Button>
    <TextBlock Margin="25,17,169.89,0" Height="34" VerticalAlignment="Top" ClipToBounds="False" LineStackingStrategy="BlockLineHeight" TextWrapping="WrapWithOverflow">Press an key on an attached keyboard, or enter text in the textbox below, to see details of the input device and the key(s) you pressed.</TextBlock>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


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


About the Authors

Steve Messer
Software Developer (Senior)
United States United States
No Biography provided

Emma Burrows
Software Developer
United Kingdom United Kingdom
Emma's first steps in programming took place at primary school over thirty years ago, thanks to a TI-99/4A and the LOGO language. Following a Master's degree in English Studies (obtained, strangely enough, with a paper on the birth of the microcomputer), Emma started her career in IT.
Over the last ten years, she has worked as a localiser, technical writer, editor, web designer, systems administrator, team leader and support engineer, before finally making the move into software development a few years ago. She is now thrilled on a daily basis that she is getting paid for writing code after doing it for free half her life!

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 9 Mar 2013
Article Copyright 2007 by Steve Messer, Emma Burrows
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid