What's in Microsoft.VisualBasic for C# Developers: Part 5 - Hardware
[This blog is part of a larger series; to find more parts in the series, please see the Series Index]
Hardware isn’t just related to computer information—like OS version and memory—but it also includes a variety of other devices, such as audio, clocks, keyboards, and many more. The Visual Basic library provides access to many of these pieces of hardware:
Audio
Microsoft.VisualBasic.Devices.Audio is an interesting class that allows you to play various sounds. It has three key functions:
- Play – This allows you to play a wave file, either by passing in the filename, a stream, or a byte array. There are several overloads to control how the audio is played.
- Stop – Stop the currently playing audio.
- PlaySystemSound – This plays the sound associated with a specified system event.
string waveFile = @"C:\Windows\winsxs\amd64_microsoft-windows-speech-userexperience_31bf3856ad364e35_6.1.7600.16385_none_77fee1b2657da663\Speech Sleep.wav";
Audio audio = new Audio();
// play full file to window
audio.Play(waveFile, Microsoft.VisualBasic.AudioPlayMode.WaitToComplete);
// play first 300ms and stop
audio.Play(waveFile);
Thread.Sleep(300);
audio.Stop();
// play sound associated with a question
audio.PlaySystemSound(System.Media.SystemSounds.Question);
Console.ReadKey();
Clock
Microsoft.VisualBasic.Devices.Clock is rather pointless—it has three properties matching existing ones:
- GmtTime equals DateTime.UtcNow
- LocalTime equals DateTime.Now
- TickCount equals Environment.TickCount
Keyboard
This provides a mix of features. First, there are three Boolean properties for modifier keys (Ctrl, Alt, and Shift), telling you if they’re pressed. Next, there are three more Boolean properties for toggle keys (Caps Lock, Scroll Lock, and Num Lock), indicating if they’re enabled.
Keyboard keyboard = new Keyboard();
Console.WriteLine("Alt pressed: {0}", keyboard.AltKeyDown);
Console.WriteLine("<kbd>Ctrl</kbd> pressed: {0}", keyboard.CtrlKeyDown);
Console.WriteLine("<kbd>Shift</kbd> pressed: {0}", keyboard.ShiftKeyDown);
Console.WriteLine("Caps Lock on: {0}", keyboard.CapsLock);
Console.WriteLine("Num Lock on: {0}", keyboard.NumLock);
Console.WriteLine("Scroll Lock on: {0}", keyboard.ScrollLock);
The interesting feature is the SendKeys method, which lets you simulate keystrokes in the active window:
Note: If you're using a console application (or something that doesn’t handle window messages), use the overload with a boolean parameter and set it to
true.
// WinForms example
keyboard.SendKeys("Hello world!");
// Console example
keyboard.SendKeys("Hello world!", true);
Mouse
Microsoft.VisualBasic.Devices.Mouse does not let you control a mouse like in The Matrix—it only provides status information via three properties:
- ButtonsSwapped – Are the left and right mouse buttons swapped?
- WheelExists – Does the mouse have a wheel?
- WheelScrollLines – How many lines does a single notch scroll?
Ports
Need to work with serial (COM) ports? Microsoft.VisualBasic.Devices.Ports simplifies this, though it doesn’t provide anything you can’t already get from System.IO.Ports.SerialPort—it just saves a few lines of code.
The OpenSerialPort method does the following:
- Creates a new System.IO.Ports.SerialPort.
- Calls Open on the port.
- Returns the port.
Three lines of code wrapped into one for convenience.
Additionally, the SerialPortNames property returns a ReadOnlyCollection