0

I'm trying to figure out how to write keyboard and mouse inputs to the port. In my code, every 15 milliseconds the mouse coordinates are written to the port.

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    writeToPort(new Point(e.X, e.Y));
}

public void writeToPort(Point coordinates)
{
    if (watch.ElapsedMilliseconds > 15)
    {
        watch = Stopwatch.StartNew();

        port.Write(String.Format("X{0}Y{1}",
        (coordinates.X / (Size.Width / 180)),
        (coordinates.Y / (Size.Height / 180))));

Then I had an int called Key that changed it's value depending on what key you pressed (w changed it's value to 1, a changed it's value to 2, s changed it's value to 3, d changed it's value to 4) and I tried to write that to the port at well.

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    writeToPort(new Point(e.X, e.Y));
}

public void writeToPort(Point coordinates)
{
    if (watch.ElapsedMilliseconds > 15)
    {
        watch = Stopwatch.StartNew();

        port.Write(String.Format("X{0}Y{1}K{2}",
        (coordinates.X / (Size.Width / 180)),
        (coordinates.Y / (Size.Height / 180)),
        Key));

What I found though, was that when the mouse was staying still, the key wasn't being written to the port.

How can I make the Key be written to the port when I press it?

sketch
  • 3
  • 2
  • What do you mean by "to the port". What port? – Dai Nov 29 '20 at 04:19
  • Instead of adding your code to the `MouseMove` event handler, try creating a new [KeyDown](https://stackoverflow.com/questions/33818896/how-to-implement-the-keyboard-key-press-in-windows-form-application) event handler, and put the code there. – John Wu Nov 29 '20 at 04:29
  • @JohnWu then it will be only triggered when keydown is happened, his problem is that, he need to write current position every 15 milliseconds regardless of an action – Vilsad P P Nov 29 '20 at 04:49
  • Use a TIMER?.... – Idle_Mind Nov 29 '20 at 04:49

1 Answers1

2

you can use a global variable and store your coordinates there, when ever the mouse is moved, on mouse move you can update this coordinates and using a timer you can write this coordinates every 15 milliseconds,

you cannot trigger writes every 15 milliseconds with mouse move event, it will trigger only when mouse is moved.

System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
Point lastCoords;
private void Form1_Load(object sender, System.EventArgs e){
   myTimer.Tick += new EventHandler(TimerEventProcessor);
   myTimer.Interval = 15;
   myTimer.Start();
}
private void TimerEventProcessor(Object myObject,
                                        EventArgs myEventArgs) {
   if(lastCoords != null)
      writeToPort(lastCoords);
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    lastCoords = new Point(e.X, e.Y);
    //writeToPort(new Point(e.X, e.Y));
}


public void writeToPort(Point coordinates)
{
   

        port.Write(String.Format("X{0}Y{1}K{2}",
            (coordinates.X / (Size.Width / 180)),
            (coordinates.Y / (Size.Height / 180)),
            Key));
     
}
Vilsad P P
  • 1,529
  • 14
  • 23
  • 1
    15ms intervals won't align with the 16.666ms frame-time that Windows uses for 60fps mouse cursor movement. Also, getting the cursor coordinates from Window Messages rather than through DirectInput/XInput will be slower (due to latency inherent in the window message pump). – Dai Nov 29 '20 at 04:52
  • Well, the question was not about the accuracy of mouse pointer, he wanted to write his last cursor position every 15 milliseconds and I was addressing that, I was just correcting his logic to accomodate that, I never said anything about using windows messages, i was asking him to use a global scoped variable (form level) and assign the value to that instead of wrting to port. If his mouse point change is not triggered in 15ms as you mentioned, he will be getting his last position. – Vilsad P P Nov 29 '20 at 05:04
  • Thank you so much, I'm very new to coding and have been struggling to get this to work, and now it does. I am extremely grateful for your help. Thank you. – sketch Nov 29 '20 at 05:45