elringus / unity-raw-input Goto Github PK
View Code? Open in Web Editor NEWWindows Raw Input wrapper for Unity game engine
Home Page: https://elringus.me/using-windows-rawinput-api-in-unity/
License: MIT License
Windows Raw Input wrapper for Unity game engine
Home Page: https://elringus.me/using-windows-rawinput-api-in-unity/
License: MIT License
is it possible to add il2cpp support thanks!
Can i use this project also with android?
GetKeyDown should indicate a singular frame press and then false until repressed.
while GetKey should indicate just a true or false of if the key is being held down.
See Input in Unity.
With the given example RawInputTest.cs, RawKeyInput.InterceptMessages is assigned in OnValidate(), which works in editor, but in a build OnValidate() doesn't get called. The OnValidate() docs specify "Called in the editor only". Not really a bug but can confuse a person if you're building off the example and don't know the limitation of OnValidate(). Great project otherwise!
Hi, thanks for making this package available !
I'm using UnityRawInput to make a screencast plugin.
Do you know if there is a way to get the real key behind OEM1, OEM2, ... and such ? Same for N0, N1, etc.
On an azerty keyboard, OEM1 is &
but it will be something else on another layout.
Hello! this is Drift_EX from twitter.
I'm trying to use this solution to have Unity fire events when not in focus, but:
In the Editor the RawInput function (button down/up functions) get called even if the button pressed is a total different one.
And, when I compile the project the RawInput doesn't work at all.
Note: I am using this with Unity 2019.1.5f1
Will this asset also allow me to see the raw input device where the input is coming from?
This can be slow due to boxing:
.NET 5+ has generic overload which doesn't involve boxing. We have to use it when Unity allows.
ref: https://stackoverflow.com/questions/13615/validate-enum-values/55028274#55028274
EDIT : I tried with a totally new project, I only have one script that is the exact script from your example project, I ticked the "run in background" option in player settings and it still doesn't register input if I click another window
Maybe the project is outdated or I didn't understand something.
I everything exactly like in your example project, everything works fine but if I lose focus, it stops working.
Here is the code where I use it
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityRawInput;
[System.Serializable]
public struct WindowSavedStruct
{
public string title;
public IntPtr hWnd;
public WindowSavedStruct(string title, IntPtr hWnd)
{
this.title = title;
this.hWnd = hWnd;
}
}
public class WindowSwitcher : MonoBehaviour
{
public bool WorkInBackground = true;
public bool InterceptMessages = false;
[DllImport("user32.dll")]
internal static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
internal static extern IntPtr SetForegroundWindow(IntPtr hWnd);
public EasySwitchMenu easySwitchMenu;
public int selectedWindowIndex = 0;
public GameObject DofusProcessUIPositionPrefab;
public GameObject gridGameobject;
public List<WindowSavedStruct> dofusWindowsList = new List<WindowSavedStruct>();
public void OnEnable()
{
RawInput.WorkInBackground = WorkInBackground;
RawInput.InterceptMessages = InterceptMessages;
RawInput.OnKeyUp += LogKeyUp;
RawInput.OnKeyDown += LogKeyDown;
RawInput.OnKeyDown += DisableIntercept;
RawInput.Start();
}
private void OnDisable()
{
RawInput.Stop();
RawInput.OnKeyUp -= LogKeyUp;
RawInput.OnKeyDown -= LogKeyDown;
RawInput.OnKeyUp -= DisableIntercept;
}
private void OnValidate()
{
// Apply options when toggles are clicked in editor.
// OnValidate is invoked only in the editor (won't affect build).
RawInput.InterceptMessages = InterceptMessages;
RawInput.WorkInBackground = WorkInBackground;
}
private void LogKeyUp(RawKey key)
{
Debug.Log("Key Up: " + key);
}
private void LogKeyDown(RawKey key)
{
Debug.Log("Key Down: " + key);
}
private void DisableIntercept(RawKey key)
{
if (RawInput.InterceptMessages && key == RawKey.Escape)
RawInput.InterceptMessages = InterceptMessages = false;
}
public void CapturedKeyPressed(RawKey key)
{
Debug.Log(key);
if (key == RawKey.W)
{
SwitchToNextWindow();
}
}
private void SwitchToNextWindow()
{
if (selectedWindowIndex < dofusWindowsList.Count - 1)
{
selectedWindowIndex++;
}
else
{
selectedWindowIndex = 0;
}
Debug.Log("Switching to window: " + dofusWindowsList[selectedWindowIndex].title);
SwitchToWindow(dofusWindowsList[selectedWindowIndex].hWnd);
}
private void SwitchToWindow(IntPtr hWnd)
{
if (hWnd != IntPtr.Zero)
{
SetForegroundWindow(hWnd);
ShowWindow(hWnd, 5);
SetForegroundWindow(easySwitchMenu.AppWindow);
Debug.Log(hWnd);
}
else
{
Debug.LogWarning("GetFocusWin() failed, hWnd is 0!");
}
}
public void RemoveClosedWindows()
{
List<WindowSavedStruct> tempWindowSaved = new List<WindowSavedStruct>();
foreach (WindowSavedStruct window in dofusWindowsList)
{
if (FindWindow(null, window.title) == IntPtr.Zero)
{
tempWindowSaved.Add(window);
}
}
foreach (WindowSavedStruct window in tempWindowSaved)
{
if (FindWindow(null, window.title) == IntPtr.Zero)
{
dofusWindowsList.Remove(window);
}
}
RefreshUI();
}
public void RefreshUI()
{
// Delete all children gameobjects from gridGameobject
foreach (Transform child in gridGameobject.transform)
{
Destroy(child.gameObject);
}
int index = 0;
foreach (WindowSavedStruct window in dofusWindowsList)
{
GameObject newDofusProcessUIPosition = Instantiate(DofusProcessUIPositionPrefab, gridGameobject.transform);
newDofusProcessUIPosition.GetComponent<DofusProcessObject>().Initialize(window.title, index, this);
index++;
}
}
}
Hi.
I love this solution ! thank you.
one question more than an issue.
is there a way to assign a RawKey from a string. the reverse of "key.ToString()"
I'd like to rebind the key mapping, so i need a way to re-assign the list of "RawKey key" on my app start-up.
Hello Great Job!
I want to ask you how can i completely hide the app from taskbar but still run in background?
I'm using your package to take keyboard input even when the unity application does not have focus. Unfortunately, I noticed that this is not woking when the active input handling is set to "Both" (old input system package + new input system package). I need the new input system to take the input from my joystick. Do you know why this is not working and how to fix it? Thanks in advance.
This is a really cool library!
wanted to ask if there is a way to get input from mouse (movement and button press)
I need the raw mouse button input to also be readable in the same manner as keyboard key presses in this project. The following resources may be useful:
https://stackoverflow.com/questions/53845288/c-sharp-hooking-to-get-raw-mouse-key-events
https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-rawmouse
I'm getting extremely slow, laggy, barely controllable mouse movement when my Unity build is running. When I run the same app on my older computers however, it runs perfectly.
The mouse movement is better when over the Unity app window, but still slow.
I've narrowed it down to the RawInput code, but again, only one machine runs it slow; the other two (substantially weaker) machines run it with RawInput perfectly.
I've tried to illustrate the difference in mouse control by drawing little circles, first inside the Unity app window, the outside. The RawInput script is inactive in the first gif, active in the second. You can see in the second how the mouse goes flying off at the end, this is a result of the latency. How can I narrow down the cause?
Hi!
I'm trying to use UnityRawInput on Unity 2019.2
When I pushing the keyboard Unity Editor or builded project just imidiately closing without any error or bug report ask
Priviat,
Do you have any leads on how to add Joystick Inputs?
I would like to receive input events from the xboxOne Controller while the app is not focused.
Best,
Just found this and started using it to detect inputs outside of unity. It works perfectly well however once I press play and test it still works fine however after pressing play again to stop the test it then seems to lag my typing on every other application apart from unity
Hi @elringus, love RawInput, it's saved my project. Just donated $10. I know you've said the project is keyboard only, but can I make a direct $150 donation for you to add mouse button functionality? I need the raw mouse buttons, but this is well beyond my coding abilities. Please let me know if you're interested, and thank you for the great asset!
I'm converting my code from Unity's input system to RawInput, but I'm having trouble with the two functions OnKeyUp and OnKeyDown. My game uses dozens of keys, how can I convert it from the Unity input system to RawInput? The sample script only has one example each for LogKeyUp and LogKeyDown. I started giving each button its own function, like in the picture below, and it's not working; all keys trigger all functions.
For example, my previous code looked like this:
if (Input.GetKeyDown(KeyCode.B)) {...}
if (Input.GetKeyUp(KeyCode.B)) {...}
if (Input.GetKeyDown(KeyCode.S)) {...}
if (Input.GetKeyUp(KeyCode.S)) {...}
etc.
How should that look in RawInput?
How can I use the package to read Mouseinput? It works perfectly with Keys but it doesn't change to true if I want to use it with my mouse.
`Debug.Log(RawKeyInput.IsKeyDown(RawKey.LeftButton));`
That doesn't work...
I want to use it in a hololens2 uwp application.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.