Skip to main content

monolith[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 the computer information, like OS version and memory, but it also includes a variety of other types of devices such as audio, clocks, keyboards and many more. The VisualBasic library provides us with access to a lot of these pieces of hardware:

Audio

Microsoft.VisualBasic.Devices.Audio is a interesting class which allows us to play various sounds it has three functions that are key:

  • Play – This allows us to play a wave file, either by passing in the file name, a stream or byte array. There are a few overloads to allow you to control how the audio is played.
  • Stop – Stop the 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 wnd
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 which match to existing properties we know and love.

  • GmtTime equals DateTime.UtcNow
  • LocalTime equals DateTime.Now
  • TickCount equals Environment.TickCount

Keyboard

This provides us with an interesting mixed bag first we have 3 Boolean properties, one for each modifier key (Ctrl, Alt & Shift) which tell us if it is being pressed. Next we have 3 Boolean properties, one for each of the toggle keys (Caps lock, scroll lock & num lock) which tells you if they are enabled.

Keyboard keyboard = new Keyboard();
Console.WriteLine("Alt pressed: {0}", keyboard.AltKeyDown);
Console.WriteLine("Ctrl pressed: {0}", keyboard.CtrlKeyDown);
Console.WriteLine("Shift pressed: {0}", keyboard.ShiftKeyDown);

Console.WriteLine("Caps on: {0}", keyboard.CapsLock);
Console.WriteLine("Numlock on: {0}", keyboard.NumLock);
Console.WriteLine("Scroll lock on: {0}", keyboard.ScrollLock);

However the interesting piece of code is the SendKeys method which actually allows you to send key strokes to the active window, basically to simulate a user typing.

There is one thing to be aware of and that is if you are using something which does not handle window messages for example a console application. In this case you must use the overload which takes a boolean as a second parameter and set this to true.

// winforms
keyboard.SendKeys("Hello world!");
// console
keyboard.SendKeys("Hello world!", true);

Mouse

mat11Microsoft.VisualBasic.Devices.Mouse does not let you control Mouse from the Matrix, it merely provides status information to you on the hardware mouse you are using via three properties:

  • ButtonsSwapped – Are the left and right mouse buttons swapped?
  • WheelExists – Does the mouse have a mouse wheel?
  • WheelScrollLines – How many lines does a single notch of the mouse wheel to scroll?

Ports

Need to work with serial (COM) ports? The Microsoft.VisualBasic.Devices.Ports provides you with a simple way to do that, however nothing it provides you do not already get from System.IO.Ports.SerialPort except saving a few lines of code.
The OpenSerialPort method does the following:

  • Creates a new System.IO.Ports.SerialPort – OpenSerialPort does contain overloads which match to the constructors for SerialPort.
  • It then calls Open on the port.
  • It returns the port.

That three lines of code wrapped into one for you.

The other item provided on Ports is the SerialPortNames property which returns a ReadOnlyCollection<String> of the names of the port. It does this by calling the GetPortNames method from SerialPort, which returns an array and then converts that result to the collection. The only time you should use this is if you really need a ReadOnlyCollection<String> as calling the GetPortNames method directly and working with the array is much faster.