<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Huddled Masses &#187; ScanCode</title>
	<atom:link href="http://huddledmasses.org/tag/scancode/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Fri, 27 Apr 2012 05:42:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>How to get the Char(acter) and VirtualKey from a WPF KeyDown event</title>
		<link>http://huddledmasses.org/how-to-get-the-character-and-virtualkey-from-a-wpf-keydown-event/</link>
		<comments>http://huddledmasses.org/how-to-get-the-character-and-virtualkey-from-a-wpf-keydown-event/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 05:02:34 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[.Net Framework]]></category>
		<category><![CDATA[Char]]></category>
		<category><![CDATA[EventHandler]]></category>
		<category><![CDATA[Interop]]></category>
		<category><![CDATA[KeyDown]]></category>
		<category><![CDATA[PInvoke]]></category>
		<category><![CDATA[ScanCode]]></category>
		<category><![CDATA[VirtualKey]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/?p=699</guid>
		<description><![CDATA[While working on my WPF PowerShell console, I&#8217;m working on implementing PSRawUserInterface, and had to implement a method called ReadKey which returns a KeyInfo object. KeyInfo is a pretty simple struct class with four properties: VirtualKeyCode Character ControlKeyState KeyDown So it doesn&#8217;t seem like it would be a real problem &#8230; just handle the KeyDown [...]]]></description>
			<content:encoded><![CDATA[	<p>While working on my <span class="caps">WPF</span> <a href="http://www.microsoft.com/powershell" title="Windows PowerShell" rel="homepage" class="zem_slink">PowerShell</a> console, I&#8217;m working on implementing <a href="http://msdn.microsoft.com/en-us/library/system.management.automation.host.pshostrawuserinterface.aspx">PSRawUserInterface</a>, and had to implement a method called <a href="http://msdn.microsoft.com/en-us/library/system.management.automation.host.pshostrawuserinterface.readkey.aspx">ReadKey</a> which returns a <a href="http://msdn.microsoft.com/en-us/library/system.management.automation.host.keyinfo.aspx">KeyInfo</a> object.  KeyInfo is a pretty simple struct class with four properties:</p>

	<ul>
		<li>VirtualKeyCode</li>
		<li>Character </li>
		<li>ControlKeyState</li>
	</ul>
	<ul>
		<li>KeyDown</li>
	</ul>

	<p>So it doesn&#8217;t seem like it would be a real problem &#8230; just handle the <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.keyboard.keydown.aspx">KeyDown</a> <a href="http://msdn.microsoft.com/en-us/library/system.windows.uielement.keydown.aspx">event</a> or <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.keyboard.previewkeydown.aspx">PreviewKeyDown</a> <a href="http://msdn.microsoft.com/en-us/library/system.windows.uielement.previewkeydown.aspx">event</a> on the <span class="caps">WPF</span> control, right? Well, no. Because all of these just use a <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.keyeventargs.aspx">KeyEventArgs</a> parameter which has a <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.key.aspx">Key</a> property which doesn&#8217;t map to a virtual key code (why isn&#8217;t the <span class="caps">WPF</span> Key enumeration in the right order? It&#8217;s ridiculous), and there&#8217;s no character information at all.</p>

	<p>Thankfully, it <strong>is</strong> possible to get this information using PInvoke, but I wouldn&#8217;t want to try to do this in <a href="http://www.microsoft.com/silverlight/default.aspx" title="Microsoft Silverlight" rel="homepage" class="zem_slink">Silverlight</a>.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' />  </p>

	<p>Here&#8217;s how it goes:</p>

	<p>First, you need to get the VirtualKey code. Thankfully, there&#8217;s a simple class called <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.keyinterop.aspx">KeyInterop</a>, which exposes a static method <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.keyinterop.virtualkeyfromkey.aspx">VirtualKeyFromKey</a> that gets us this information.</p>

	<p>Then, we need to get the character. This is much trickier.  First we have to <a href="http://msdn.microsoft.com/en-us/library/ms646299.aspx">get the keyboard state</a> and then we have to <a href="http://msdn.microsoft.com/en-us/library/ms646306.aspx">map that VirtualKey</a> we got in the first step to a ScanCode, and finally, convert all of that <a href="http://msdn.microsoft.com/en-us/library/ms646320.aspx">to Unicode</a>, because .Net doesn&#8217;t really speak <span class="caps">ASCII</span>  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' /> </p>

	<p>The rest of the code is pretty clear, I hope&#8230;<span id="more-699"></span> you can find the <em>current</em> version of <a href="https://poshconsole.svn.codeplex.com/svn/trunk/Huddled/Interop/Keyboard.cs">Keyboard.cs</a> on CodePlex in the PoshCode source repository, but to make this complete, here&#8217;s the version as it stands now:</p>

	<div class="csharp code csharp" style="font-family:monospace;"><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Management.Automation.Host</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Runtime.InteropServices</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Windows.Input</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF;">namespace</span> Huddled.<span style="color: #0000FF;">Interop</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> Keyboard<br />
&nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;The set of valid MapTypes used in MapVirtualKey</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;remarks&gt;&lt;/remarks&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">enum</span> MapType <span style="color: #008000;">:</span> <span style="color: #FF0000;">uint</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;uCode is a virtual-key code and is translated into a scan code.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// If it is a virtual-key code that does not distinguish between left- and</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// right-hand keys, the left-hand scan code is returned.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// If there is no translation, the function returns 0.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;remarks&gt;&lt;/remarks&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MAPVK_VK_TO_VSC <span style="color: #008000;">=</span> 0x0,<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;uCode is a scan code and is translated into a virtual-key code that</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// does not distinguish between left- and right-hand keys. If there is no</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// translation, the function returns 0.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;remarks&gt;&lt;/remarks&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MAPVK_VSC_TO_VK <span style="color: #008000;">=</span> 0x1,<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;uCode is a virtual-key code and is translated into an unshifted</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// character value in the low-order word of the return value. Dead keys (diacritics)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// are indicated by setting the top bit of the return value. If there is no</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// translation, the function returns 0.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;remarks&gt;&lt;/remarks&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MAPVK_VK_TO_CHAR <span style="color: #008000;">=</span> 0x2,<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Windows NT/2000/XP: uCode is a scan code and is translated into a</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// virtual-key code that distinguishes between left- and right-hand keys. If</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// there is no translation, the function returns 0.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;remarks&gt;&lt;/remarks&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MAPVK_VSC_TO_VK_EX <span style="color: #008000;">=</span> 0x3,<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Not currently documented</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;remarks&gt;&lt;/remarks&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MAPVK_VK_TO_VSC_EX <span style="color: #008000;">=</span> 0x4,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// The ToUnicode function translates the specified virtual-key code and keyboard state </span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// to the corresponding Unicode character or characters. To specify a handle to the keyboard layout </span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// to use to translate the specified code, use the ToUnicodeEx function.</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// </span></div>

<div class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/4d89ee6c-d3c1-45fd-a043-eaef925688b6/" title="Zemified by Zemanta"><img class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=4d89ee6c-d3c1-45fd-a043-eaef925688b6" alt="Reblog this post [with Zemanta]" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/how-to-get-the-character-and-virtualkey-from-a-wpf-keydown-event/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

