Skip to content

Commit

Permalink
Merge pull request #22 from luojunyuan/master
Browse files Browse the repository at this point in the history
New target framework to net7
  • Loading branch information
mfakane authored Aug 14, 2023
2 parents c47f610 + b32ce8b commit bd064c4
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 10 deletions.
127 changes: 127 additions & 0 deletions RawInput.Sharp.SimpleExample.Win32/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
using Linearstar.Windows.RawInput;
using System.ComponentModel;
using System.Runtime.InteropServices;


var WindowClass = "HelperWindowClass";
var wind_class = new WNDCLASS
{
lpszClassName = Marshal.StringToHGlobalUni(WindowClass),
lpfnWndProc = (hWnd, msg, wParam, lParam) =>
{
const int WM_INPUT = 0x00FF;

// You can read inputs by processing the WM_INPUT message.
if (msg == WM_INPUT)
{
// Create an RawInputData from the handle stored in lParam.
var data = RawInputData.FromHandle(lParam);

// You can identify the source device using Header.DeviceHandle or just Device.
var sourceDeviceHandle = data.Header.DeviceHandle;
var sourceDevice = data.Device;

// The data will be an instance of either RawInputMouseData, RawInputKeyboardData, or RawInputHidData.
// They contain the raw input data in their properties.
switch (data)
{
case RawInputMouseData mouse:
Console.WriteLine(mouse.Mouse);
break;
case RawInputKeyboardData keyboard:
Console.WriteLine(keyboard.Keyboard);
break;
case RawInputHidData hid:
Console.WriteLine(hid.Hid);
break;
}
}

// The normal way to quit is sending WM_CLOSE message to the window
// if (msg == 0x0002) { // WM_DESTORY
// PostQuitMessage(0);
// return nint.Zero;
// }

// handle the messages here
return DefWindowProc(hWnd, msg, wParam, lParam);
}
};

ushort classAtom = RegisterClassW(ref wind_class);

if (classAtom == 0)
throw new Win32Exception();

const uint WS_EX_NOACTIVATE = 0x08000000;
const uint WS_POPUP = 0x80000000;
IntPtr hWnd = CreateWindowExW(
WS_EX_NOACTIVATE,
WindowClass,
"",
WS_POPUP,
0, 0, 0, 0,
IntPtr.Zero,
IntPtr.Zero,
IntPtr.Zero,
IntPtr.Zero
);

if (hWnd == IntPtr.Zero)
throw new Win32Exception();


// Get the devices that can be handled with Raw Input.
var devices = RawInputDevice.GetDevices();

// register the keyboard device and you can register device which you need like mouse
RawInputDevice.RegisterDevice(HidUsageAndPage.Keyboard,
RawInputDeviceFlags.ExInputSink | RawInputDeviceFlags.NoLegacy, hWnd);


// Message loop
while (GetMessage(out var msg, IntPtr.Zero, 0, 0))
{
TranslateMessage(msg);
DispatchMessage(msg);
}


[DllImport("user32.dll")]
static extern bool GetMessage(out IntPtr lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax);
[DllImport("user32.dll")]
static extern bool TranslateMessage(in IntPtr lpMsg);
[DllImport("user32.dll")]
static extern IntPtr DispatchMessage(in IntPtr lpMsg);


[DllImport("user32.dll", SetLastError = true)]
static extern ushort RegisterClassW([In] ref WNDCLASS lpWndClass);

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr CreateWindowExW(uint dwExStyle,
[MarshalAs(UnmanagedType.LPWStr)] string lpClassName,
[MarshalAs(UnmanagedType.LPWStr)] string lpWindowName,
uint dwStyle, int x, int y, int nWidth, int nHeight,
IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);

[DllImport("user32.dll")]
static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);


delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

[StructLayout(LayoutKind.Sequential)]
struct WNDCLASS
{
public uint style;
public WndProc lpfnWndProc;
public int cbClsExtra;
public int cbWndExtra;
public IntPtr hInstance;
public IntPtr hIcon;
public IntPtr hCursor;
public IntPtr hbrBackground;
public string lpszMenuName;
public nint lpszClassName;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\RawInput.Sharp\RawInput.Sharp.csproj" />
</ItemGroup>

</Project>
16 changes: 11 additions & 5 deletions RawInput.Sharp.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28803.352
# Visual Studio Version 17
VisualStudioVersion = 17.7.33920.267
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RawInput.Sharp", "RawInput.Sharp\RawInput.Sharp.csproj", "{47C40BEA-5191-46A3-BE27-4B7A1B725C31}"
EndProject
Expand All @@ -11,11 +11,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RawInput.Sharp.SimpleExample", "RawInput.Sharp.SimpleExample\RawInput.Sharp.SimpleExample.csproj", "{F129A5A1-95EA-41CF-98E3-ECB61BEB5040}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RawInput.Sharp.SimpleExample", "RawInput.Sharp.SimpleExample\RawInput.Sharp.SimpleExample.csproj", "{F129A5A1-95EA-41CF-98E3-ECB61BEB5040}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RawInput.Sharp.SimpleExample.WPF", "RawInput.Sharp.SimpleExample.WPF\RawInput.Sharp.SimpleExample.WPF.csproj", "{63B773C2-7995-4A6A-AAE8-C0DEF73454F9}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RawInput.Sharp.SimpleExample.WPF", "RawInput.Sharp.SimpleExample.WPF\RawInput.Sharp.SimpleExample.WPF.csproj", "{63B773C2-7995-4A6A-AAE8-C0DEF73454F9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RawInput.Sharp.DigitizerExample", "RawInput.Sharp.DigitizerExample\RawInput.Sharp.DigitizerExample.csproj", "{59C646E7-8E08-4B06-9F2E-B66962448BA2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RawInput.Sharp.DigitizerExample", "RawInput.Sharp.DigitizerExample\RawInput.Sharp.DigitizerExample.csproj", "{59C646E7-8E08-4B06-9F2E-B66962448BA2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RawInput.Sharp.SimpleExample.Win32", "RawInput.Sharp.SimpleExample.Win32\RawInput.Sharp.SimpleExample.Win32.csproj", "{E3FBC4E2-70F1-447A-980B-0351E9D525A7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -39,6 +41,10 @@ Global
{59C646E7-8E08-4B06-9F2E-B66962448BA2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{59C646E7-8E08-4B06-9F2E-B66962448BA2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{59C646E7-8E08-4B06-9F2E-B66962448BA2}.Release|Any CPU.Build.0 = Release|Any CPU
{E3FBC4E2-70F1-447A-980B-0351E9D525A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E3FBC4E2-70F1-447A-980B-0351E9D525A7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E3FBC4E2-70F1-447A-980B-0351E9D525A7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E3FBC4E2-70F1-447A-980B-0351E9D525A7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
4 changes: 4 additions & 0 deletions RawInput.Sharp/MarshalEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@ namespace Linearstar.Windows.RawInput;

static class MarshalEx
{
#if NET7_0_OR_GREATER
public static int SizeOf<T>() => Marshal.SizeOf<T>();
#else
public static int SizeOf<T>() => Marshal.SizeOf(typeof(T));
#endif
}
2 changes: 1 addition & 1 deletion RawInput.Sharp/Native/DeviceInstanceHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Linearstar.Windows.RawInput.Native;

public bool Equals(DeviceInstanceHandle other) => value.Equals(other.value);

public override bool Equals(object obj) =>
public override bool Equals(object? obj) =>
obj is DeviceInstanceHandle other &&
Equals(other);

Expand Down
2 changes: 1 addition & 1 deletion RawInput.Sharp/Native/HidDeviceHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Linearstar.Windows.RawInput.Native;

public bool Equals(HidDeviceHandle other) => value.Equals(other.value);

public override bool Equals(object obj) =>
public override bool Equals(object? obj) =>
obj is HidDeviceHandle other &&
Equals(other);

Expand Down
2 changes: 1 addition & 1 deletion RawInput.Sharp/RawInput.Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageTags>Windows RawInput</PackageTags>
<RepositoryUrl>https://github.com/mfakane/rawinput-sharp</RepositoryUrl>
<Nullable>enable</Nullable>
<TargetFrameworks>netstandard1.1;net461</TargetFrameworks>
<TargetFrameworks>netstandard1.1;net461;net7.0</TargetFrameworks>
</PropertyGroup>

</Project>
4 changes: 2 additions & 2 deletions RawInput.Sharp/RawInputHidData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public class RawInputHidData : RawInputData

public HidButtonSetState[] ButtonSetStates =>
Device != null
? Hid.ToHidReports().SelectMany(report => Device?.Reader.ButtonSets.Select(x => x.GetStates(report))).ToArray()
? Hid.ToHidReports().SelectMany(report => Device.Reader.ButtonSets.Select(x => x.GetStates(report))).ToArray()
: new HidButtonSetState[0];

public HidValueSetState[] ValueSetStates =>
Device != null
? Hid.ToHidReports().SelectMany(report => Device?.Reader.ValueSets.Select(x => x.GetStates(report))).ToArray()
? Hid.ToHidReports().SelectMany(report => Device.Reader.ValueSets.Select(x => x.GetStates(report))).ToArray()
: new HidValueSetState[0];

protected RawInputHidData(RawInputHeader header, RawHid hid)
Expand Down

0 comments on commit bd064c4

Please sign in to comment.