Skip to content

Commit

Permalink
Read and write mouse wheel data
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersMalmgren committed Feb 17, 2014
1 parent ed1e5d3 commit d706b0a
Showing 1 changed file with 33 additions and 31 deletions.
64 changes: 33 additions & 31 deletions FreePIE.Core.Plugins/MousePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using FreePIE.Core.Contracts;
using FreePIE.Core.Plugins.Strategies;
using SlimDX.DirectInput;
using SlimDX.RawInput;

namespace FreePIE.Core.Plugins
{
Expand All @@ -15,6 +16,8 @@ public class MousePlugin : Plugin
// Mouse position state variables
private double deltaXOut;
private double deltaYOut;
private int wheel;
public const int WheelMax = 120;

private DirectInput directInputInstance = new DirectInput();
private Mouse mouseDevice;
Expand Down Expand Up @@ -70,16 +73,6 @@ public override string FriendlyName
get { return "Mouse"; }
}

public override bool GetProperty(int index, IPluginProperty property)
{
return false;
}

public override bool SetProperties(Dictionary<string, object> properties)
{
return true;
}

static private MouseKeyIO.MOUSEINPUT MouseInput(int x, int y, uint data, uint t, uint flag)
{
var mi = new MouseKeyIO.MOUSEINPUT {dx = x, dy = y, mouseData = data, time = t, dwFlags = flag};
Expand All @@ -89,12 +82,12 @@ static private MouseKeyIO.MOUSEINPUT MouseInput(int x, int y, uint data, uint t,
public override void DoBeforeNextExecute()
{
// If a mouse command was given in the script, issue it all at once right here
if ((int)deltaXOut != 0 || (int)deltaYOut != 0)
if ((int)deltaXOut != 0 || (int)deltaYOut != 0 || wheel != 0)
{

var input = new MouseKeyIO.INPUT[1];
input[0].type = MouseKeyIO.INPUT_MOUSE;
input[0].mi = MouseInput((int)deltaXOut, (int)deltaYOut, 0, 0, MouseKeyIO.MOUSEEVENTF_MOVE);
input[0].mi = MouseInput((int)deltaXOut, (int)deltaYOut, (uint)wheel, 0, MouseKeyIO.MOUSEEVENTF_MOVE | MouseKeyIO.MOUSEEVENTF_WHEEL);

MouseKeyIO.SendInput(1, input, Marshal.SizeOf(input[0].GetType()));

Expand All @@ -107,6 +100,8 @@ public override void DoBeforeNextExecute()
{
deltaYOut = deltaYOut - (int)deltaYOut;
}

wheel = 0;
}

currentMouseState = null; // flush the mouse state
Expand All @@ -121,15 +116,7 @@ public double DeltaX
deltaXOut = deltaXOut + value;
}

get
{
// Retrieve the mouse state only once per iteration to avoid getting
// zeros on subsequent calls
if (currentMouseState == null)
currentMouseState = mouseDevice.GetCurrentState();

return currentMouseState.X;
}
get { return CurrentMouseState.X; }
}

public double DeltaY
Expand All @@ -139,26 +126,30 @@ public double DeltaY
deltaYOut = deltaYOut + value;
}

get { return CurrentMouseState.Y; }
}

public int Wheel
{
get { return CurrentMouseState.Z; }
set { wheel = value; }

}

private MouseState CurrentMouseState
{
get
{
// Retrieve the mouse state only once per iteration to avoid getting
// zeros on subsequent calls
if (currentMouseState == null)
currentMouseState = mouseDevice.GetCurrentState();

return currentMouseState.Y;
return currentMouseState;
}
}

public bool IsButtonDown(int index)
{

// Retrieve the mouse state only once per iteration to avoid getting
// zeros on subsequent calls
if (currentMouseState == null)
currentMouseState = mouseDevice.GetCurrentState();

return currentMouseState.IsPressed(index);
return CurrentMouseState.IsPressed(index);
}

public bool IsButtonPressed(int button)
Expand Down Expand Up @@ -242,6 +233,11 @@ public class MouseGlobal : UpdateblePluginGlobal<MousePlugin>
{
public MouseGlobal(MousePlugin plugin) : base(plugin) { }

public int wheelMax
{
get { return MousePlugin.WheelMax; }
}

public double deltaX
{
get { return plugin.DeltaX; }
Expand All @@ -254,6 +250,12 @@ public double deltaY
set { plugin.DeltaY = value; }
}

public int wheel
{
get { return plugin.Wheel; }
set { plugin.Wheel = value; }
}

public bool leftButton
{
get { return plugin.IsButtonDown(0); }
Expand Down

2 comments on commit d706b0a

@drowhunter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

excellent

@AndersMalmgren
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.