InputSimulator在调试时有效,但在构建程序时无效

编程入门 行业动态 更新时间:2024-10-28 07:21:28
本文介绍了InputSimulator在调试时有效,但在构建程序时无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的项目中,我使用inputsimulator,当以Visual Studio作为管理员运行Visual Studio时效果很好,但是当我将其构建为.exe时,即使以管理员身份运行它也无法正常工作.这是我的代码

In my project im using inputsimulator and it works great when visual studio is ran as an administrator, but when i build it into a .exe it doesn't work even when i run it as administrator. here's my code

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click AutoSaveTimer.Enabled = True Try System.Threading.Thread.Sleep(50) GameConnection.SendKeyTo(Keys.OemSemicolon) System.Threading.Thread.Sleep(2000) GameConnection.SendKeyTo(Keys.K) System.Threading.Thread.Sleep(50) GameConnection.SendKeyTo(Keys.Enter) Catch AutoSaveExeption As GameException If AutoSaveExeption.GameErrorCode = GameError.GAME_ERR_SENDMSG Then ' Send message error - connection to Game lost. ' MessageBox.Show("cant make a connection.... can't autosave sadly", AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation) SimConnectionBar.BackColor = Color.Red End If End Try End Sub

它确实将焦点发送到我指定的窗口,但不发送键击

it does send focus to the window i specify but it doesn't send the keystrokes

推荐答案

在发送任何输入之前尝试使用SetForegroundWindow,以确保您的游戏确实具有焦点.应该在发送输入之前在您的方法中调用SetForegroundWindow.

Try using SetForegroundWindow before sending any input to ensure your game does in fact have focus.The call to SetForegroundWindow should be made in your method just before sending the input.

<DllImport("user32.dll")> _ Public Shared Function SetForegroundWindow(hWnd As IntPtr) As Boolean End Function Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ` AutoSaveTimer.Enabled = True Try 'Find the handle to the game. This can do it by searching for the process. Dim p As System.Diagnostics.Process() = System.Diagnostics.Process.GetProcessesByName("notepad") 'search for process notepad If p.Length > 0 Then 'check if window was found 'bring notepad to foreground SetForegroundWindow(p(0).MainWindowHandle) End If System.Threading.Thread.Sleep(50) GameConnection.SendKeyTo(Keys.OemSemicolon) System.Threading.Thread.Sleep(2000) GameConnection.SendKeyTo(Keys.K) System.Threading.Thread.Sleep(50) GameConnection.SendKeyTo(Keys.Enter) Catch AutoSaveExeption As GameException If AutoSaveExeption.GameErrorCode = GameError.GAME_ERR_SENDMSG Then ' Send message error - connection to Game lost. ' MessageBox.Show("cant make a connection.... can't autosave sadly", AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation) SimConnectionBar.BackColor = Color.Red End If End Try end sub

更多推荐

InputSimulator在调试时有效,但在构建程序时无效

本文发布于:2023-11-13 21:33:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585376.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:但在   程序   InputSimulator

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!