unityでの、sendinputが上手に動作しません。unityのexeファイルを作成し、 exeを起動してwindowsへキーストローク出力がしたい。
できるだけ簡単に実装したい。
keybd_eventではスクリプトでのキーストロークの検知はできたが、exeに落とし込んでキーストロークの出力を試したが反応なし。
keybd_eventでの使用は推奨されていないのでsendinputでの実装を考えたが、
exeファイルの作り方がおかしかったのでkeybd_eventの出力ができなかったのかなとも未だにわだかまりが。
今回はunityでのsendinputの実装を可能にしたい。
unityでの参考書物があれば教えていただきたい。unity以外は読みつくしたと思う。
回答お願いします。
できれば完全に動くコードが欲しい。
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Runtime.InteropServices; public class check1 : MonoBehaviour { [DllImport("user32.dll")] private extern static void SendInput(uint nInputs, ref INKey pInputs , int cbsize); [StructLayout(LayoutKind.Sequential)] private struct KEYBDINPUT { public byte wVk; public short wScan; public int dwFlags; public int time; public int dwExtraInfo; }; [StructLayout(LayoutKind.Sequential)] struct INKey { public int type; // 0 = INPUT_MOUSE, // 1 = INPUT_KEYBOARD, // 2 = INPUT_HARDWARE public KeyIN ks; } [StructLayout(LayoutKind.Explicit)] private struct KeyIN { [FieldOffset(0)] public KEYBDINPUT ki; }; private const int INPUT_KEYBOARD = 0x1; [DllImport("user32.dll", EntryPoint = "MapVirtualKeyA")] private extern static int MapVirtualKey( int wCode, int wMapType); // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.K)) { Debug.Log("押した"); const int num = 2; INKey[] ingo = new INKey[num]; ingo[0].type = INPUT_KEYBOARD; ingo[0].ks.ki.wVk = 0x41; ingo[0].ks.ki.wScan = 0x4; ingo[0].ks.ki.dwFlags = 0x1 | 0x0; SendInput(num, ref ingo[0], Marshal.SizeOf(ingo[0])); if (Input.GetKey(KeyCode.A)) { Debug.Log("記念日"); ingo[1].type = INPUT_KEYBOARD; ingo[1].ks.ki.wVk = 0x41; ingo[1].ks.ki.wScan = 0x4; ingo[1].ks.ki.dwFlags = 0x1 | 0x2; SendInput(num, ref ingo[0], Marshal.SizeOf(ingo[0])); } } } }
回答2件
あなたの回答
tips
プレビュー