Solved

VB5: Making a hotkey to activate a function

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

895 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

17 Experts available now in Live!

Get 1:1 Help Now