Solved

VB5: Making a hotkey to activate a function

Posted on 2000-02-25
4
232 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 100 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now