While I was trying to figure something else out code wise , my mind began to wander. I started thinking about a cheap man’s pinball machine. Now this has been an off again on again project of mine for the past few months. But yesterday I thought about how many 3.5 SBCs that I have and what I could do with them. Originally I wanted to take one throw it inside of a desktop pinball machine and have some crazy serial connections going back and forth to control everything. I then found that the SBCs are over kill.
So, yesterday the idea popped in my head again, but more for haha sake, I wanted to see if I could make the LEDS on a keyboard flash using C#. And in doing this I could use the current that would come from the LEDS to power a relay and turn on the Solenoids/Flippers. Just a concept. Nothing really extreme or solid, just a simple proof of concept(I’ve been in to those a lot recently.)
So I coded it out as a Console Window application using 1 C# file and the command line to compile it. (That way I couldn’t lazy my way out and use help files.) Here’s what I have.
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace popsknot.griz.keyFlasher
{
public class keyFlasher
{
[DllImport("user32.dll")]
static extern ushort GetKeyState(short nVirtKey);
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
public const ushort keyDownBit = 0x80;
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
private static int counter = 10;
private static bool rFlipper = false;
private static bool lFlipper = false;
public static void Main(string[] args)
{
if (Control.IsKeyLocked(Keys.Scroll))
{
keybd_event(0x91, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(0x91, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
if (Control.IsKeyLocked(Keys.NumLock))
{
keybd_event(0x90, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(0x90, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
while (counter !=0)
{
if (IsKeyPressed(Keys.Escape))
{
counter = 0;
}
//Right Flipper
if ((System.Windows.Forms.Control.ModifierKeys & Keys.Control) == Keys.Control)
{
if (!rFlipper)
{
Console.WriteLine("Trigger Right Flipper" + 0x90);
keybd_event(0x91, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(0x91, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
rFlipper = true;
}
}
else
{
if (rFlipper == true)
{
keybd_event(0x91,0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(0x91, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
rFlipper = false;
}
}
//Left Flipper
if ((System.Windows.Forms.Control.ModifierKeys & Keys.Alt) == Keys.Alt)
{
if (!lFlipper)
{
Console.WriteLine("Trigger Left Flipper" + 0x90);
keybd_event(0x90, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(0x90, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
lFlipper = true;
}
}
else
{
if (lFlipper == true)
{
keybd_event(0x90, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(0x90, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
lFlipper = false;
}
}
}
}
public static bool IsKeyPressed(Keys key)
{
return ((GetKeyState((short)key) & keyDownBit) == keyDownBit);
}
}
}
Also keep in mind that I did about 2 minutes of testing on this on my development machine. That means that there is a chance I may be doing something wrong and it could cause damage to something. So, use at your own risk.