- Home /
Question by
arthurconnor100 · Jun 21, 2019 at 09:29 PM ·
inputsendmessagegetkey
Send Keys to Background Window
I am trying to make a customizable on-screen keyboard and need it to send each pressed key to the background window.
I have tried both the PostMessage and the SendKeys.Send function without any luck. I don't get any error messages, just nothing happens when I click the key.
Any help would be greatly appreciated on how to get this to work.
Here's my latest attempt:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
[SerializeField]
class SendMessages
{
[DllImport("user32.dll")]
public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
public static void sendKeystroke()
{
const uint WM_KEYDOWN = 0x100;
const uint WM_KEYUP = 0x0101;
IntPtr hWnd;
string processName = "Notepad";
Process[] processList = Process.GetProcesses();
foreach (Process P in processList)
{
if (P.ProcessName.Equals(processName))
{
IntPtr edit = P.MainWindowHandle;
PostMessage(edit, WM_KEYDOWN, (IntPtr)(Keys.B), IntPtr.Zero);
}
}
}
}
public class B_Button : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown(0))
{
SendMessages.sendKeystroke();
}
}
}
Comment
Your answer
Follow this Question
Related Questions
if-else for Input.GetKey 1 Answer
Change view if key is pressed 4 Answers
Input.GetKey behaving strangely 2 Answers
How can I check if a key/button is not (!) being held down? 1 Answer
Gamepad input enum eType not working 0 Answers