Results 1 to 3 of 3

Thread: Key press from outside of program

  1. #1
    Guest

    Question

    Help!!!

    I wrote a prog that calls the screensaver and was wondering is there was a way to run the program when the user presses the F12 key.

    Is there some code I can write that when the prog is installed it can associate the F12 key with running the program????

    Thanks

    !!!Stumps!!!

  2. #2
    Guest
    To run a program from within your App, all you have to do is use the Shell Function.

    Code:
    RetVal = Shell("Myexe.EXE", 1)

  3. #3
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    You could create a HotKey, i.e.

    In a Module:
    Code:
    Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal MSG As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    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 SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    
    Private Const WM_HOTKEY = &H312
    Private Const GWL_WNDPROC = (-4)
    
    Private lPrevWndProc As Long
    
    Private Function SubClassedWnd(ByVal hWnd As Long, ByVal MSG As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        If MSG = WM_HOTKEY And wParam = 1 Then
            'HotKey Pressed
            'Do What You Want Here.
            MsgBox "Starting ScreenSaver... (Replace me with your Code).", vbInformation + vbOKOnly, "Example"
        End If
        SubClassedWnd = CallWindowProc(lPrevWndProc, hWnd, MSG, wParam, lParam)
    End Function
    
    Public Sub SetHotKey(ByVal hWnd As Long)
        lPrevWndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf SubClassedWnd)
        Call RegisterHotKey(hWnd, 1, 0, vbKeyF12)
    End Sub
    
    Public Sub RemoveHotKey(ByVal hWnd As Long)
        Call UnregisterHotKey(hWnd, 1)
        Call SetWindowLong(hWnd, GWL_WNDPROC, lPrevWndProc)
    End Sub
    In a Form:
    Code:
    Private Sub Form_Load()
        SetHotKey hWnd
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        RemoveHotKey hWnd
    End Sub

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