VB2005 maintain focus on hidden app

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jontcaz
    New Member
    • Aug 2007
    • 3

    VB2005 maintain focus on hidden app

    Good Day,
    I have an application that will send text out of a serial port if i hold down the "1" key for 2 seconds. I am currently just turning a chime on and off but want to expand this to the ability to turn my amplifier on/off etc. I want this to be running in the background but to respond at any time by just hitting a certain key. The program works great one time when i execute, but goes into hide and i never see it again. Any suggestions are appreciated.

    Public Class frmKeyCatcher
    Dim WithEvents serialPort As New IO.Ports.Serial Port

    Private Sub DataReceived( _
    ByVal sender As Object, _
    ByVal e As System.IO.Ports .SerialDataRece ivedEventArgs) _
    Handles serialPort.Data Received

    txtDataReceived .Invoke(New myDelegate(Addr essOf updateTextBox), _
    New Object() {})
    End Sub

    Public Delegate Sub myDelegate()
    Public Sub updateTextBox()
    With txtDataReceived
    .SelectionColor = Color.Red
    .AppendText(ser ialPort.ReadExi sting)
    .ScrollToCaret( )
    End With
    End Sub

    Private Sub frmKeyCatcher_F ormClosing(ByVa l sender As Object, ByVal e As System.Windows. Forms.FormClosi ngEventArgs) Handles Me.FormClosing
    serialPort.Clos e()
    End Sub


    Private Sub frmKeyCatcher_K eyDown(ByVal sender As Object, ByVal e As System.Windows. Forms.KeyEventA rgs) Handles Me.KeyDown
    If e.KeyCode = 49 Then
    Timer1.Start()

    End If
    End Sub

    Private Sub frmKeyCatcher_K eyUp(ByVal sender As Object, ByVal e As System.Windows. Forms.KeyEventA rgs) Handles Me.KeyUp
    If e.KeyCode = 49 Then
    Timer1.Stop()
    End If
    End Sub

    Private Sub Timer1_Tick(ByV al sender As Object, ByVal e As System.EventArg s) Handles Timer1.Tick
    Me.Show()
    Me.BackColor = Color.LightGray
    If serialPort.IsOp en Then
    serialPort.Clos e()
    End If
    Try
    With serialPort
    .PortName = "COM3"
    .BaudRate = 96000
    .Parity = IO.Ports.Parity .None
    .DataBits = 8
    .StopBits = IO.Ports.StopBi ts.One

    End With

    serialPort.Open ()
    Timer4.Start()
    Catch ex As Exception
    MsgBox(ex.ToStr ing)
    End Try

    End Sub

    Private Sub Timer2_Tick(ByV al sender As Object, ByVal e As System.EventArg s) Handles Timer2.Tick
    Me.Hide()
    Timer2.Stop()
    End Sub

    Private Sub Timer4_Tick(ByV al sender As Object, ByVal e As System.EventArg s) Handles Timer4.Tick
    Try
    serialPort.Writ e("C")
    With txtDataReceived
    .SelectionColor = Color.Black
    .AppendText("C" & vbCrLf)
    .ScrollToCaret( )

    End With
    Me.BackColor = Color.white
    Catch ex As Exception

    End Try
    Timer4.Stop()
    Timer2.Start()
    End Sub


    End Class
  • Robbie
    New Member
    • Mar 2007
    • 180

    #2
    Originally posted by jontcaz
    ... I want this to be running in the background but to respond at any time by just hitting a certain key. ...
    Hi. The code which you posted does not look like VB6 code...

    I have a 'Declare Function' which you can use in VB6. I don't know if you can use it in .NET though, because I'm not really advanced with it.

    [code=vb]Private Declare Function GetAsyncKeyStat e Lib "user32" (ByVal vKey As Long) As Integer[/code]

    You use it by giving it a keycode to check for. It seems to check the keyboard directly, rather than the program having to have focus for it to work. If it gives back -32767, then the key is pressed, otherwise it's not.

    So it would be easier to set up a constant of that, e.g.
    [code=vb]Const KeyPressed As Integer = -32767[/code]

    Then check the function against that constant, e.g. to check if the Space key is being pressed:

    if GetAsyncKeyStat e(vbKeySpace) = KeyPressed then
    'The key is down
    else
    'It's not being pressed down
    end if

    As I said, that's VB6 code, but I hope you can apply it to what you need it for. And I haven't tested, but there is equivalent code in another programming language I'm familiar with, to check the keyboard directly (and I assume this is what this function does), and it works without the program having to have focus.

    EDIT: So you'd need to check regularly with a timer or while-doevents-wend loop or such, whether or not the key is currently down.

    Comment

    • jontcaz
      New Member
      • Aug 2007
      • 3

      #3
      [QUOTE=Robbie]Hi. The code which you posted does not look like VB6 code...

      Hey Robbie, thanks for the reply, i guess i should have posted this in .net forum eh? I will try your ideas in 6 to see if that works.

      Comment

      Working...