[C#.NET] How to make my hardware-driver work in the OS events?

Hey guys!

Working with C#.NET in simple game projects and other stuff for about 4 years.
Now I just started with Arduino and managed to kick through all the starter kit projects.

My youngest project currently is to create a simple hardware control device to control a simple game.

So i created a simple on/off switch on the Arduino Uno which sends bytes through .NET's "System.IO.Ports.SerialPort" - Library.

Now I have to create my own little driver software.

It really just should be controllable to a minimum - just to test things out.

Heres the code:

public class ButtonBoy
	{
		public delegate void ButtonPressHandler();
		public event ButtonPressHandler ButtonEvent;

		SerialPort serialPort = new SerialPort("COM3", 9600);

		public ButtonBoy()
		{
			serialPort.Open();
		}


		public void CheckButton()
		{
			int pressed = 0;

			pressed = serialPort.ReadByte();
			if (pressed == 1)
			{
				ButtonEvent();
			}
		}
	}

So whats my biggest issue here?

When I load this simple driver-api to one of my other projects to test things out - I actually don't have a way to receive the event like common Windows OS Keys. As far as I know its because the OS has its own thread iterating through the common key events or mouse events.

What I need to do always with this state of the API is to create a loop to iterate through my CheckButton-Method just to launch the event of the pressed button on my hardware device.

I'm sure that's not how things work.

So i was curious if anybody knows how to let my driver work just like other drivers work. In fact I only want to care about the ButtonEvent handling and not if the CheckButton-Method is being called all the time.

I hope you get my idea here?

greets Charlie

That looks like a PC programming question.

This is an Arduino forum.

...R

Ok sorry then!

Hello,

I'm not sure to understand. I think you want to run code on your pc when the hardware button is pressed, why not use the DataReceived event?

			pressed = serialPort.ReadByte();

Don't you think that you should check that there is anything to read, first? Don't you think that you should check that the port is even open, first?