As mentioned by Spiked3 there are a few ways to hit the ground running with C#.
Firstly, as detailed by Majenko, you will need an application running on your PC and some code on the Arduino to interact with this. For the desktop application, the two easiest ways to get started with C# are with WinForms, or XNA/MonoGame. WinForms is targeted at Desktop application development; think legacy desktop apps. Although it is not the current defacto standard (replaced largely by WPF) it is very simple and akin to Swing in Java to use. MonoGame and XNA are targeted at video game development. The latter would be my preference as it lends itself more towards real time applications, and has a game loop much like the Arduino so it is very easy to start using. The caveat is that there is more setup involved, but you will probably find this the friendliest and best supported.
Firstly, The Code! So for the Winforms Route, We have something like this:
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
Debug.WriteLine(" Key Up! ({0})", e.KeyCode);
// TODO: Talk with Arduino
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
Debug.WriteLine(" Key Press! ({0})", e.KeyChar);
// TODO: Talk With Arduino
}
}
}
To get to this point, fire up a new winforms project, and open up the 'Form1' code (the default window which is added to the project) add the methods as outlined above. You will need to go to the designer afterwards and add events for on KeyPressand on KeyUp to tie these functions to the Window. You can do this by clicking the Window, then opening the Properties Panel, and then clicking the lightening bolt. You will see a list of event names; find the corresponding events (Key Press and Key Up) and associate them with these methods.
From here, you can perform some logic when a button is either pressed or released.
The monogame or XNA approach is a bit simpler to follow as there is no hidden magic; but may be more intimidating if you're not familiar with C# and the project type.
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System.Diagnostics;
namespace GameName1
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
KeyboardState currentState;
KeyboardState previousState;
public Game1() : base()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// Get state management started
currentState = Keyboard.GetState();
base.Initialize();
}
protected override void Update(GameTime gameTime)
{
// Listen for exit command
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// Update the state of your keyboard object
previousState = currentState;
currentState = Keyboard.GetState();
// Detect Keypress of key 'F'
if (currentState.IsKeyDown(Keys.F) && previousState.IsKeyUp(Keys.F))
{
// F Key has been pressed
Debug.WriteLine(" Key Pressed! ({0})", 'F');
//TODO Talk to Arduino
}
// Detect Keyrelease of key 'F'
if (currentState.IsKeyUp(Keys.F) && previousState.IsKeyDown(Keys.F))
{
// F Key has been pressed
Debug.WriteLine(" Key Released! ({0})", 'F');
//TODO Talk to Arduino
}
base.Update(gameTime);
}
}
}
In either case, you will need to get yourself set up first. Get a compy of Visual Studio Community from the link below, and install MonoGame if desired. Then do some C# tutorials to get comfortable in your new environment.
Lastly, you will want to talk with your Arduino. This can be done by implementing a compatible Serial interface between your desktop application and your Arduino. See the tutorial below to get started!
I hope this information is useful. There is a very broad range of topics mentioned here, and it may seem overwhelming at first - but C# is a very idiosyncratic language in that it permits rapid development in many different scenarios, and I think that you will find it rewarding to conquer the learning curve.