0

I have a simple program. There is a Label (=0) and when i press "W" The Label gets + 1 everytime. Now i want to run it in the background, but when i tab out the user Input won't work. So im searching for a way to use User Input even tho i tabbed out or minimized the Programm. I really dont now what to do.

I hope you Can help me. Thanks for reading and Thanks for future answers

i cant post my question so dont mind me down here

  • 1
    you can't do this entirely within your program. once the program looses focus, your keyboard and mouse inputs are no longer being piped to it, so it can't hear them. Instead look into options like AutohotKey. – Frank Thomas Jun 08 '19 at 23:05
  • Thx, but how can i use Autohotkey and VB together? I dont want to learn the language of Ahk... – Thaltech Jun 09 '19 at 15:35
  • you would program a hotkey in AHK to send input to your VB program. it doesn't have to be AHK, thats just the first thing I'd look at. either way, you need a means to set up a hotkey in your windows session itself, so it will be captured regardless of what app has focus. – Frank Thomas Jun 09 '19 at 20:55

1 Answers1

1

So i found an Easy way WITH AHK: I made The Hotkey with Ahk and when ever i press it, it'll write into an txt file, then VB is just reading the Txt file. Problem solved. CODES: AHK:

 run, Test.exe
 number := 0
file := Fileopen("input.txt" , "w")

    file.write(number)
    file.Close()
^r::
 number := number + 1

file := Fileopen("input.txt" , "w")

    file.write(number)
    file.Close()

 return

VB:

Public Class Form1
Public NumberVB

Private Sub Update_Tick(sender As Object, e As EventArgs) Handles Update.Tick
    NumberVB = My.Computer.FileSystem.ReadAllText("input.txt")
    Label1.Text = "How often you pressed the Hotkey: " & NumberVB
End Sub

Private Sub Form1_Closed(sender As Object, e As EventArgs) Handles Me.Closed
    Dim arrProcess() As Process = System.Diagnostics.Process.GetProcessesByName("Autohotkey")

    For Each p As Process In arrProcess
        p.Kill()
    Next
End Sub
End Class
  • how often is tick defined as? you probably don't want to read a file,no matter how short more than once per second, – Frank Thomas Jun 10 '19 at 03:05
  • you may want to put in some exception handling and perhaps lock checking. file IO can have some nasty consequences during race conditions, even in .Net, and you would not want a 2-3 second issue with file availability to crash your program. just ignore that tick, and wait to refresh your label until the next one. – Frank Thomas Jun 10 '19 at 03:30
  • @FrankThomas i could, but it works with no Errors and it updates instantly. however, thanks for the comment. EDIT: the Tick is 150 – Thaltech Jun 26 '19 at 21:49