|
-
May 14th, 2004, 09:58 AM
#1
Thread Starter
New Member
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?
-
May 14th, 2004, 10:09 AM
#2
does your app have a form? or is it something that is supposed to just run in the background?
-
May 14th, 2004, 10:11 AM
#3
Thread Starter
New Member
it doesn't need to be seen
it can run in the background.
-
May 14th, 2004, 10:21 AM
#4
Fanatic Member
Try this
VB Code:
Option Explicit
Private blnPushedTwicce As Boolean
Private Sub Form_Load()
'this will catch keys that are pressed
Me.KeyPreview = True
blnPushedTwicce = False
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
'Chech to see if F11 is pressed
If KeyCode = 122 Then
If blnPushedTwicce = False Then
'Perform action for first press
blnPushedTwicce = True
Else
'Perform action for second press
blnPushedTwicce = False
End If
End If
End Sub
hope this helps
Useful Links
.Net
#Develop, GhostDoc, CodeKeep , be.PINVOKE, Good Code Snippet Site
Krypton Toolkit, XPCC / XP Common Controls, QSS Windows Forms Components
VB.COM
VB.Classic Help File, MB Controls, MZTools, ADO Stored Procedure Generator add-in,
-
May 14th, 2004, 10:37 AM
#5
Thread Starter
New Member
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.
-
May 14th, 2004, 10:55 AM
#6
Thread Starter
New Member
it doesn't seem to work
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
-
May 14th, 2004, 11:08 AM
#7
Need-a-life Member
Re: it doesn't seem to work
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
Emiliano F. Martín
If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
-
May 14th, 2004, 11:50 AM
#8
Use the GetAsyncKeyState API:
VB Code:
Option Explicit
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal dwMessage As Long) As Integer
Private Sub Form_Load()
Timer1.Interval = 200
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Dim keyresult As Long
keyresult = GetAsyncKeyState(vbKeyF6)
If keyresult And &H8000 Then
MsgBox "F6 Pressed"
End If
End Sub
-
May 17th, 2004, 10:42 AM
#9
Thread Starter
New Member
still not sure
My code does work if it is in the foreground, but if I leave it in the background (not visible) then the F11 or F12 or other function keys appear to affect other applications and never hit my app.
I tried the one posted by Negative 0, but the Timer1.Interval wasn't defined.
-
May 17th, 2004, 10:49 AM
#10
Need-a-life Member
Re: still not sure
Originally posted by les_stockton
My code does work if it is in the foreground, but if I leave it in the background (not visible) then the F11 or F12 or other function keys appear to affect other applications and never hit my app.
I tried the one posted by Negative 0, but the Timer1.Interval wasn't defined.
You need to add a Timer control to your app to try that code.
Emiliano F. Martín
If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
-
May 17th, 2004, 10:50 AM
#11
You need to place a timer on the form.
-
May 17th, 2004, 10:56 AM
#12
-
May 18th, 2004, 10:13 AM
#13
Thread Starter
New Member
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
-
May 19th, 2004, 08:48 AM
#14
Try lowering the timer interval.
-
May 19th, 2004, 09:37 AM
#15
Fanatic Member
Why don't you use the RegisterHotkey API? It's been made for things like this. 
VB Code:
Private Const MOD_ALT = &H1
Private Const MOD_CONTROL = &H2
Private Const MOD_SHIFT = &H4
Private Const PM_REMOVE = &H1
Private Const WM_HOTKEY = &H312
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type Msg
hWnd As Long
Message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type
Private Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Private Declare Function UnregisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long) As Long
Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
Private Declare Function WaitMessage Lib "user32" () As Long
Private bCancel As Boolean
Private Sub ProcessMessages()
Dim Message As Msg
'loop until bCancel is set to True
Do While Not bCancel
'wait for a message
WaitMessage
'check if it's a HOTKEY-message
If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
'minimize the form
WindowState = vbMinimized
End If
'let the operating system process other events
DoEvents
Loop
End Sub
Private Sub Form_Load()
'KPD-Team 2000
'URL: [url]http://www.allapi.net/[/url]
Dim ret As Long
bCancel = False
'register the Ctrl-F hotkey
ret = RegisterHotKey(Me.hWnd, &HBFFF&, MOD_CONTROL, vbKeyF)
'show some information
Me.AutoRedraw = True
Me.Print "Press CTRL-F to minimize this form"
'show the form and
Show
'process the Hotkey messages
ProcessMessages
End Sub
Private Sub Form_Unload(Cancel As Integer)
bCancel = True
'unregister hotkey
Call UnregisterHotKey(Me.hWnd, &HBFFF&)
End Sub
Author for Visual Basic Web Magazine
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|