Function Keys (catching them)?
I'm very new to VB.
I need to create a very simple program that acts on function key 11 (F11).
All it does is when F11 is pushed, it does an MCI command "Set cdaudio door open"
Upon another F11 is does "Set cdaudio door closed"
and then goes back to the first waiting for another F11.
That's all.
It just toggles between the two.
Since I'm real new at this, can someone tell me the easiest way to do this, or if there's an example out there that I can use?
it doesn't need to be seen
it can run in the background.
how do I make a background program
So does anyone have the stubbed out code for a headless (background) program or is there a way creating an application straight from VB to say that I don't need a frame or anything displayed?
It can be on the tray or background or whatever.
Re: it doesn't seem to work
Quote:
Originally posted by les_stockton
I seem to have it in the background. I can see that it's running within taskmgr.
It doesn't seem to trap the keys though.
Option Explicit
Private blnPushedTwice As Boolean
Private Sub Form_Load()
Me.KeyPreview = True
blnPushedTwice = False
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 119 Then
If blnPushedTwice = False Then
' perform action for first press
MsgBox "F8 is pushed"
blnPushedTwice = True
Else
' Perform action for second press
MsgBox "F8 is pushed again"
blnPushedTwice = False
End If
End If
End Sub
This works. Why do you say it doesn't?
VB Code:
Option Explicit
Private Sub Form_Load()
Me.KeyPreview = True
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Static blnPushedTwice As Boolean
If KeyCode = vbKeyF8 Then
If blnPushedTwice = False Then
' perform action for first press
MsgBox "F8 is pushed"
blnPushedTwice = True
Else
' Perform action for second press
MsgBox "F8 is pushed again"
blnPushedTwice = False
End If
End If
End Sub
why do I have to hit the button a few times to take affect?
why do I have to push the F6 button a few times (sometimes) to get it to work?
Option Explicit
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal dwMessage As Long) As Integer
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
(ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal _
uReturnLength As Long, ByVal hwndCallback As Long) As Long
Dim doorclosed As Long
Private Sub Form_Load()
Timer1.Interval = 200
Timer1.Enabled = True
doorclosed = 1
End Sub
Private Sub Timer1_Timer()
Dim keyresult As Long
Dim errRtn As Long
Dim returnString As String
keyresult = GetAsyncKeyState(vbKeyF6)
If keyresult And &H8000 Then
MsgBox "f6 pressed"
If doorclosed = 1 Then
errRtn = mciSendString("set cdaudio door open", returnString, 0, 0)
doorclosed = 0
Else
errRtn = mciSendString("set cdaudio door closed", returnString, 0, 0)
doorclosed = 1
End If
End If
End Sub