Arduino and Visualstudio C#

Hello programmers, I have an arduino uno and I want to create mouse click with visual studio... When I press a button connected with the arduino read a serial string variable ex."LeftClick" with visual studio and the simulate the mouse click with it... The problem is that I can not do this so I asked for your help :slight_smile:

DrunkProgrammer:
Hello programmers, I have an arduino uno and I want to create mouse click with visual studio... When I press a button connected with the arduino read a serial string variable ex."LeftClick" with visual studio and the simulate the mouse click with it... The problem is that I can not do this so I asked for your help :slight_smile:

I still have no idea what you want.

You should get an Arduino that can act like a HID (Human Interface Device).

How to simulate Mouse Click in C#? might be of help if you want to use the uno. And msdn SerialPort class reference for the serial port communication.

Yes you can use Visual C# and read the serial output from your Arduino. If you using the MS Express editions 2008 or 2010 you can use the SerialPort class.

Eg

using System.IO.Ports;

// Inside your class

string[] portsavailable;

Serialport port1 = new SerialPort ("COM11", 9600, Parity.None, 8, StopBits.One);

//Inside your Form1

try
{
portsavailable = SerialPort.
GetPortNames ();

}
catch (Exception ex)
{

Messagebox.Show ("Check Arduino connected");
}

//Inside your mouse click or button clicl event

//Check if your port is open

if (port1.IsOpen){

port1.Close ();
port1.Open ();

}
else
{
port1.Open ();

}

string s= " ";

// Read your port
s = s + "\n" + port1.ReadLine ();

//Do something with your string s

// You will need to modify the above code.

// It should help you make some progress.

Goodluck

You want to simulate mouse clicks. Arduino leonardo or micro can emulate mouse to send clicks. Why did you mention visual studio?

Thanks for your answers! Mr.liudr in my country the banks are closed and the payments outside the country banneds due to capital controls, so I will have to simulate the mouse clicks with my arduino uno!

Are you saying you can't buy an arduino leonardo because you can't pay sellers outside your country? Where did you get your uno? If you can't get any other arduino hardware, can you get a used ps/2 keyboard or a male mini din plug? Emulating a ps/2 keyboard may be the way to go.

First of all, Mr.liudr I bought my arduino uno before the capitals. Secondly, I can not understand what is your exact goal here to help someone that needs assistance or ask for his personal payments?

I wasn't asking for money. How did you infer that? I was saying that there are more elegant way to make an arduino uno emulate a keyboard than involving some C# etc.. i.e. make it emulate a ps/2 keyboard with a ps/2 keyboard plug. My goal has been helping you make arduino send key presses to a PC but you made it convoluted to a point that I'm not interested in helping anymore. Google arduino ps/2 emulator and educate yourself the subject.

I have write this code and it is not simulating mouse clicks what might the problem be?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO.Ports;

namespace ArduinoVR {
public partial class Form1 : Form {

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;

string[] portsavailable;

SerialPort port1 = new SerialPort("COM4", 9600);

public Form1() {
InitializeComponent();
try {
portsavailable = SerialPort.
GetPortNames();

} catch (Exception ex) {
MessageBox.Show(ex.Message,"Check Arduino connected");
}
}

private void Form1_Load(object sender, EventArgs e) {
if (port1.IsOpen) {

port1.Close();
port1.Open();

} else {
port1.Open();

}

string s = " ";

s = port1.ReadLine();
textBox1.Text += s;
if (s == "LeftClick") {
DoMouseClick();
textBox1.Text += "Reading";
}
}

public void DoMouseClick() {
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
uint x = Convert.ToUInt32(X);
uint y = Convert.ToUInt32(Y);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}
}
}

You might be better of asking C# specific questions in the C# section on MSDN forums.

And they will probably also ask you to post code in code tags and properly define what 'does not work' means :wink: