473,416 Members | 1,559 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

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.SerialPort

Private Sub DataReceived( _
ByVal sender As Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles serialPort.DataReceived

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

Public Delegate Sub myDelegate()
Public Sub updateTextBox()
With txtDataReceived
.SelectionColor = Color.Red
.AppendText(serialPort.ReadExisting)
.ScrollToCaret()
End With
End Sub

Private Sub frmKeyCatcher_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
serialPort.Close()
End Sub


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

End If
End Sub

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

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.Show()
Me.BackColor = Color.LightGray
If serialPort.IsOpen Then
serialPort.Close()
End If
Try
With serialPort
.PortName = "COM3"
.BaudRate = 96000
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One

End With

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

End Sub

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

Private Sub Timer4_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer4.Tick
Try
serialPort.Write("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
Aug 22 '07 #1
2 1446
Robbie
180 100+
... 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.

Expand|Select|Wrap|Line Numbers
  1. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
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.
Expand|Select|Wrap|Line Numbers
  1. Const KeyPressed As Integer = -32767
Then check the function against that constant, e.g. to check if the Space key is being pressed:

if GetAsyncKeyState(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.
Aug 22 '07 #2
[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.
Aug 23 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: PK9 | last post by:
On my page, I want to set the focus to the top of the page (where I have a message in a label) after a button is clicked. How can I use javascript on the server side to set the focus either to a...
5
by: Paul E Collins | last post by:
I have a Web form with a number of text boxes. If the user submits the page with some text boxes filled in, those then become plain text (with accompanying <input type="hidden"> fields for when the...
17
by: Neil Ginsberg | last post by:
OK, this is a stupid thing, but I can't seem to get this to work. I have a form with a subform (in continuous form view). A combo box on the main form has code in the AfterUpdate event which adds a...
10
by: Galen Somerville | last post by:
Going from VB6 (RAD) to VB2005 (SAD) The documentation always shows graphics drawing in a paint event like Private Sub Pictcolor_Paint(ByVal sender As Object, ByVal e _ As...
2
by: Rich | last post by:
Greetings, I observed that in VB2005 the designer generated code is hidden. The initialize code appears to be stored in a different module than the Form's class module. But the form's class...
1
by: zwieback89 | last post by:
Hi, I have a org tree with hierarchical display of employees built using classic asp and vbscript. I also have list of radio buttons for report names. I have 1 select box with dates in it....
15
by: Aalaan | last post by:
I am presently a user of classic vb6 and hang out on those newsgroups. Some of you may be aware that there is a very anti MS and vb2005 feeling there. I have tried to get them to tell me which...
11
by: sheldonlg | last post by:
I have a problem with not seeing the current focus while in a particular screen in a my app. The focus does not show for a type=text or textarea. I have tried to reproduce this problem in a test...
5
by: Dave Rado | last post by:
Hi I have been the following code by Freefind to use on my search page: <form action="http://search.freefind.com/find.html" method="get" accept-charset="utf-8" target="_self"> <input...
6
by: buntyindia | last post by:
Hi, I have a page. There is a section 'sidebar' where bulleted text is there. Just after first bullet there is a hidden IFRAME. That go visible on clicking of that bullet text. My problem is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.