Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB5: Making a hotkey to activate a function

Posted on 2000-02-25
4
Medium Priority
?
247 Views
Last Modified: 2010-05-02
I need code that will tell windows that when a specific key, or combo, such as F12 for example, is pushed, that it should execute code from my program running in the background.  I can do this is my app has focus without problem...but I need it to work when it doesn't have focus and is running in the background minimized, or in the system tray.

And also is there a way to pull the names of all the active programs running under windows?
0
Comment
Question by:ChrisK
4 Comments
 
LVL 2

Author Comment

by:ChrisK
ID: 2560468
By "names" I mean the title in the application menu bar for use with the AppActivate command.
0
 
LVL 14

Accepted Solution

by:
mcrider earned 400 total points
ID: 2560512
To monitor keys, you can try the GetKeyState API... Here is the documentation on the API: http://msdn.microsoft.com/library/psdk/winui/keybinpt_4z51.htm 


As for your second question, this article shows how to to pull the names of all the active programs running under windows.

VB3 How to Get Windows Master List (Task List)
http://support.microsoft.com/support/kb/articles/Q78/0/01.ASP?LNG=ENG&SA=MSDN&FR=1 

It's a 16-bit example, but here are the 32-bit api calls used in the program:

Declare Function GetWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long


You can also get the pathname of the programs executing...
HOWTO: List Running Processes
http://support.microsoft.com/support/kb/articles/Q187/9/13.ASP?LNG=ENG&SA=MSDN&FR=1 

0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2560951
To register a hot key in Windows, instead of using GetKeyState (takes CPU-power)..

Private Const WM_SETHOTKEY = &H32
 Private Declare Function SendMessageByLong Lib "user32" Alias "SendMessageA"
 (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As
 Long) As Long

 Public Enum echkModifierKeys
     HOTKEYF_SHIFT = &H1
     HOTKEYF_CONTROL = &H2
     HOTKEYF_ALT = &H4
     HOTKEYF_EXT = &H8
     HOTKEYF_SHIFTCONTROL = &H3
     HOTKEYF_ALTSHIFT = &H5
     HOTKEYF_CONTROLALT = &H6
     HOTKEYF_CONTROLALTSHIFT = &H7
 End Enum

 Public Function SetHotKey( _
         ByVal hWnd As Long, _
         ByVal eKeyCode As VBRUN.KeyCodeConstants, _
         ByVal eModifier As echkModifierKeys _
     ) As Boolean
 Dim iR As Long
 Dim lKey As Long
     ' wParam is a word with the LoByte set to
     ' the key code and the HiByte set to the modifier:
     lKey = (eKeyCode And &HFF&) Or ((eModifier And &HFF&) * &H100&)
     iR = SendMessageByLong(hWnd, WM_SETHOTKEY, lKey, 0)
     Select Case iR
     Case 2
         Err.Raise 20001, App.EXEName & ".SetHotKey", "Hot key previously
 assigned"
     Case 1
         ' success
         SetHotKey = True
     Case 0
         Err.Raise 20002, App.EXEName & ".SetHotKey", "Invalid window for Hot
 key"
     Case -1
         Err.Raise 20003, App.EXEName & ".SetHotKey", "Invalid Hot key"
     Case Else
         Err.Raise 20004, App.EXEName & ".SetHotKey", "Failed to set Hot key"
     End Select
 End Function


(code from http://www.vbaccelerator.com/codelib/comctl/hotkey.htm)
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2561088
"VBnet Subclassing: System-Wide Keyboard Trapping with RegisterHotKey"

http://www.mvps.org/vbnet/code/subclass/registerhotkey.htm
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question