Results 1 to 15 of 15

Thread: Function Keys (catching them)?

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2004
    Location
    Oklahoma
    Posts
    12

    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?

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    does your app have a form? or is it something that is supposed to just run in the background?

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2004
    Location
    Oklahoma
    Posts
    12

    it doesn't need to be seen

    it can run in the background.

  4. #4
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667
    Try this

    VB Code:
    1. Option Explicit
    2. Private blnPushedTwicce As Boolean
    3. Private Sub Form_Load()
    4.     'this will catch keys that are pressed
    5.     Me.KeyPreview = True
    6.     blnPushedTwicce = False
    7. End Sub
    8.  
    9. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    10.     'Chech to see if F11 is pressed
    11.     If KeyCode = 122 Then
    12.         If blnPushedTwicce = False Then
    13.             'Perform action for first press
    14.             blnPushedTwicce = True
    15.         Else
    16.             'Perform action for second press
    17.             blnPushedTwicce = False
    18.         End If
    19.     End If
    20.  
    21.  
    22. End Sub

    hope this helps

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2004
    Location
    Oklahoma
    Posts
    12

    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.

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2004
    Location
    Oklahoma
    Posts
    12

    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

  7. #7
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    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:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.    Me.KeyPreview = True
    5. End Sub
    6.  
    7. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    8.     Static blnPushedTwice As Boolean
    9.    
    10.     If KeyCode = vbKeyF8 Then
    11.         If blnPushedTwice = False Then
    12.             ' perform action for first press
    13.             MsgBox "F8 is pushed"
    14.             blnPushedTwice = True
    15.         Else
    16.             ' Perform action for second press
    17.             MsgBox "F8 is pushed again"
    18.             blnPushedTwice = False
    19.         End If
    20.     End If
    21. 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.

  8. #8
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    Use the GetAsyncKeyState API:

    VB Code:
    1. Option Explicit
    2.  
    3.  
    4. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal dwMessage As Long) As Integer
    5.  
    6.  
    7. Private Sub Form_Load()
    8. Timer1.Interval = 200
    9. Timer1.Enabled = True
    10. End Sub
    11.  
    12. Private Sub Timer1_Timer()
    13. Dim keyresult As Long
    14. keyresult = GetAsyncKeyState(vbKeyF6)
    15.     If keyresult And &H8000 Then
    16.          MsgBox "F6 Pressed"
    17.     End If
    18.    
    19. End Sub

  9. #9

    Thread Starter
    New Member
    Join Date
    May 2004
    Location
    Oklahoma
    Posts
    12

    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.

  10. #10
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    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.

  11. #11
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    You need to place a timer on the form.

  12. #12
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Originally posted by Negative0
    You need to place a timer on the form.
    Beat ya'
    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.

  13. #13

    Thread Starter
    New Member
    Join Date
    May 2004
    Location
    Oklahoma
    Posts
    12

    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

  14. #14
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    Try lowering the timer interval.

  15. #15
    Fanatic Member TheVader's Avatar
    Join Date
    Oct 2002
    Location
    Rotterdam, the Netherlands
    Posts
    871
    Why don't you use the RegisterHotkey API? It's been made for things like this.
    VB Code:
    1. Private Const MOD_ALT = &H1
    2. Private Const MOD_CONTROL = &H2
    3. Private Const MOD_SHIFT = &H4
    4. Private Const PM_REMOVE = &H1
    5. Private Const WM_HOTKEY = &H312
    6. Private Type POINTAPI
    7.     x As Long
    8.     y As Long
    9. End Type
    10. Private Type Msg
    11.     hWnd As Long
    12.     Message As Long
    13.     wParam As Long
    14.     lParam As Long
    15.     time As Long
    16.     pt As POINTAPI
    17. End Type
    18. Private Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
    19. Private Declare Function UnregisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long) As Long
    20. 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
    21. Private Declare Function WaitMessage Lib "user32" () As Long
    22. Private bCancel As Boolean
    23. Private Sub ProcessMessages()
    24.     Dim Message As Msg
    25.     'loop until bCancel is set to True
    26.     Do While Not bCancel
    27.         'wait for a message
    28.         WaitMessage
    29.         'check if it's a HOTKEY-message
    30.         If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
    31.             'minimize the form
    32.             WindowState = vbMinimized
    33.         End If
    34.         'let the operating system process other events
    35.         DoEvents
    36.     Loop
    37. End Sub
    38. Private Sub Form_Load()
    39.     'KPD-Team 2000
    40.     'URL: [url]http://www.allapi.net/[/url]
    41.     'E-Mail: [email][email protected][/email]
    42.     Dim ret As Long
    43.     bCancel = False
    44.     'register the Ctrl-F hotkey
    45.     ret = RegisterHotKey(Me.hWnd, &HBFFF&, MOD_CONTROL, vbKeyF)
    46.     'show some information
    47.     Me.AutoRedraw = True
    48.     Me.Print "Press CTRL-F to minimize this form"
    49.     'show the form and
    50.     Show
    51.     'process the Hotkey messages
    52.     ProcessMessages
    53. End Sub
    54. Private Sub Form_Unload(Cancel As Integer)
    55.     bCancel = True
    56.     'unregister hotkey
    57.     Call UnregisterHotKey(Me.hWnd, &HBFFF&)
    58. End Sub
    Author for Visual Basic Web Magazine

    My articles on the Web Browser Control:
    Using the Web Browser Control & Using the DHTML Document Object Model

    The examples referenced in the articles can be found here:

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width