tag:blogger.com,1999:blog-38129028804911914662024-11-08T07:26:46.882-08:00Sharper DevelopmentAdventures in CodeNicohttp://www.blogger.com/profile/01951356961094424282noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-3812902880491191466.post-9185629025971207102013-02-27T12:21:00.000-08:002013-02-27T12:21:10.219-08:00What most schools don´t teach, but every single one should.<iframe allowfullscreen="" frameborder="0" height="480" src="http://www.youtube.com/embed/nKIu9yen5nc" width="853"></iframe>Nicohttp://www.blogger.com/profile/01951356961094424282noreply@blogger.com0tag:blogger.com,1999:blog-3812902880491191466.post-83047041069026060752013-01-09T03:29:00.002-08:002013-02-27T12:39:11.016-08:00Replacing Win XP Accessibility FeaturesHow to get the input from a serial port (Real hardware or USB) into an application that only handles keyboard inputs?<br />
<br />
Earlier Windows Versions had an accessibility feature, that would allow you to do just that, by handling serial inputs as if they were keystrokes. This feature however was discontinued in more recent Windows versions. (Vista and above)<br />
<br />
So a tool was needed, that would provide the functionality described above.<br />
<br />
This surely should not be a problem in .NET and as it turns out it is not. Central to this is the<i> "SendKeys.SendWait()" </i>Method, provided in the framework as part of the <i>"System.Windows.Forms"</i> namespace. <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.sendwait(v=vs.80).aspx" rel="nofollow" target="_blank">MSDN-Article</a><br />
<!-- code formatted by http://manoli.net/csharpformat/ -->
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> SendWait ( <span class="kwrd">string</span> keys )</pre>
This method takes a string and basically posts it to the operating system as a series of keystrokes. There are however a couple of characters that need to be treated specially because they act as control characters to the SendKeys.SendWait() Method. Namely these are: ^, ~, +, %, (, ), [, ]<br />
<br />
In order to use the method for arbitrarily defined strings, these characters need to be put into curly brackets. To send a '+' for example you would need to call the method like this:<br />
<!-- code formatted by http://manoli.net/csharpformat/ -->
<pre class="csharpcode">SendKeys.SendWait(<span class="str">"{+}"</span>);</pre>
I wrote a method to do this kind of reformatting for me:
<!-- code formatted by http://manoli.net/csharpformat/ -->
<br />
<!-- code formatted by http://manoli.net/csharpformat/ -->
<pre class="csharpcode"> <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">string</span> Reformat(<span class="kwrd">string</span> original)
{
<span class="kwrd">var</span> sb = <span class="kwrd">new</span> StringBuilder();
<span class="kwrd">foreach</span> (<span class="kwrd">char</span> c <span class="kwrd">in</span> original)
{
<span class="kwrd">switch</span> (c)
{
<span class="kwrd">case</span> <span class="str">'+'</span>: <span class="kwrd">case</span> <span class="str">'^'</span>: <span class="kwrd">case</span> <span class="str">'~'</span>: <span class="kwrd">case</span> <span class="str">'%'</span>:
<span class="kwrd">case</span> <span class="str">'('</span>: <span class="kwrd">case</span> <span class="str">')'</span>: <span class="kwrd">case</span> <span class="str">'['</span>: <span class="kwrd">case</span> <span class="str">']'</span>:
sb.AppendFormat(<span class="str">"{{{0}}}"</span>, c);
<span class="kwrd">break</span>;
<span class="kwrd">default</span>:
sb.Append(c);
<span class="kwrd">break</span>;
}
}
<span class="kwrd">return</span> sb.ToString();
}
</pre>
Note that this uses the AppendFormat() Method of a StringBuilder. (Namespace "System.Text") To add curly brackets in the "Format" context you have to type double brackets, because the curly bracket itself is used as an escape sequence in format-strings.<br />
<br />
The relevant part in the program looks like this:
<!-- code formatted by http://manoli.net/csharpformat/ -->
<br />
<pre class="csharpcode"> <span class="rem">/// <summary></span>
<span class="rem">/// Event thrown whenever the serial port received data</span>
<span class="rem">/// </summary></span>
<span class="rem">/// <param name="sender"></param></span>
<span class="rem">/// <param name="e"></param></span>
<span class="kwrd">private</span> <span class="kwrd">void</span> PortOnDataReceived(<span class="kwrd">object</span> sender, SerialDataReceivedEventArgs e)
{
while (_port.BytesToRead > 0)
{
<span class="rem">// PostKeys</span>
<span class="kwrd">var</span> original = _port.ReadExisting();
<span class="rem">// Reformat string to fit SendKeys.SendWait()</span>
<span class="kwrd">var</span> reformattedString = Reformat(original);
<span class="kwrd">try</span>
{
SendKeys.SendWait(reformattedString);
}
<span class="rem">// Handle exception caused if keys are sent to an application</span>
<span class="rem">// not handling keys</span>
<span class="kwrd">catch</span>(InvalidOperationException)
{
}
}
}</pre>
I will make the complete project available on<span style="font-size: large;"> <a href="https://sourceforge.net/projects/serialporttokey/" target="_blank">Sourceforge</a>. </span>Comments, questions and suggestions are, as always, very welcome.Nicohttp://www.blogger.com/profile/01951356961094424282noreply@blogger.com17Kiel, Deutschland54.3232927 10.12276520000000354.0270902 9.4773182000000027 54.6194952 10.768212200000004