-
I'm trying to use the if (deltaY < 0)
{
deltaY = -120;
}
else if (deltaY > 0)
{
deltaY = 120;
}
var union = new INPUT._Anonymous_e__Union
{
mi = new MOUSEINPUT
{
dwFlags = MOUSE_EVENT_FLAGS.MOUSEEVENTF_WHEEL,
dx = 0,
dy = 0,
time = 0,
mouseData = deltaY,
dwExtraInfo = 0
}
};
var input = new INPUT
{
type = INPUT_TYPE.INPUT_MOUSE,
Anonymous = union
};
var inputs = new Span<INPUT>();
inputs.Fill(input);
SendInput(inputs, Marshal.SizeOf(typeof(INPUT))); But it doesn't work. That's why I have this question. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
Ultimately I think your error is that you create a span of zero length, and then try to 'fill' it, but nothing will happen because it's empty. -var inputs = new Span<INPUT>();
-inputs.Fill(input);
+var inputs = new Span<INPUT>(ref input); But that relies on a new constructor for .NET 7. If you're not that lucky, this will also work: Span<INPUT> inputs = stackalloc INPUT[1];
inputs[0] = input; I would also touch up a bit of the other syntax, leaving me with: var input = new INPUT
{
type = INPUT_TYPE.INPUT_MOUSE,
Anonymous =
{
mi = new MOUSEINPUT
{
dwFlags = MOUSE_EVENT_FLAGS.MOUSEEVENTF_WHEEL,
dx = 0,
dy = 0,
time = 0,
mouseData = deltaY switch { < 0 => -120, > 0 => 120, 0 => 0 },
dwExtraInfo = 0
},
},
};
#if NET7_0_OR_GREATER
Span<INPUT> inputs = new(ref input);
#else
Span<INPUT> inputs = stackalloc INPUT[1];
inputs[0] = input;
#endif
PInvoke.SendInput(inputs, Marshal.SizeOf(typeof(INPUT))); I haven't tested this, but I'm optimistic it'll work. :) |
Beta Was this translation helpful? Give feedback.
Ultimately I think your error is that you create a span of zero length, and then try to 'fill' it, but nothing will happen because it's empty.
But that relies on a new constructor for .NET 7. If you're not that lucky, this will also work:
I would also touch up a bit of the other syntax, leaving me with: