By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,501 Members | 2,817 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,501 IT Pros & Developers. It's quick & easy.

Keep form active?

P: 21
Hello,

Is it possible to keep a form active, also when clicking on another application? I don't mean in the foreground, because I got that already, but now when I click on another application, my form becomes inactive.

Already thanks for the help.
Jan 4 '08 #1
Share this Question
Share on Google+
9 Replies


lotus18
100+
P: 866
Hello,

Is it possible to keep a form active, also when clicking on another application? I don't mean in the foreground, because I got that already, but now when I click on another application, my form becomes inactive.

Already thanks for the help.
I don't think it would be possible (i think). BTW, for what purpose?
Jan 4 '08 #2

QVeen72
Expert 100+
P: 1,445
Hello,

Is it possible to keep a form active, also when clicking on another application? I don't mean in the foreground, because I got that already, but now when I click on another application, my form becomes inactive.

Already thanks for the help.
Hi,

What Version of VB?
you want it to be on Top of all other windows...?

Regards
Veena
Jan 4 '08 #3

P: 21
It's because the script isn't noticing keypresses while it's inactive, and it would be handy if I can use the keyboard for navigating between different screens or applications. I can open other applications now, but I cant go back, except when using alt-tab. So it works but I can be improved.
Jan 4 '08 #4

P: 21
It is VB6.0, and it doesn't need to be on top, but it needs to be active. Another inactive application may be on top.
Jan 4 '08 #5

Ali Rizwan
100+
P: 927
It is VB6.0, and it doesn't need to be on top, but it needs to be active. Another inactive application may be on top.
Use timer and code

Form1.setfocus

or want code for setting window always on top to other all windows??

REGARDS

>> ALI <<
Jan 5 '08 #6

Expert 5K+
P: 8,434
Windows sends keypresses to the window which has the focus. The only way (as far as I know) that you could see them when you don't, is to use the API to intercept the windows messages being sent to the other application. This topic has been covered a number of times. Just try searching on "keylogger". You'll find that we're not very keen on helping people build them, though.
Jan 7 '08 #7

P: 21
Thanks for the tip, I'm going to try it with RegisterHotKey from the user32.dll.
Jan 7 '08 #8

Expert 5K+
P: 8,434
Thanks for the tip, I'm going to try it with RegisterHotKey from the user32.dll.
Let us know how it turns out.
Jan 8 '08 #9

P: 21
I got it all worked out now and it works.

I got a module called modHotkey.bas that holds the following script:

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Private Declare Function RegisterHotKey Lib "user32" _
  4.     (ByVal hwnd As Long, ByVal id As Long, _
  5.     ByVal fsModifiers As Long, ByVal vk As Long) As Long
  6. Private Declare Function UnregisterHotKey Lib "user32" _
  7.     (ByVal hwnd As Long, ByVal id As Long) As Long
  8.  
  9. Public Enum ModConst
  10.     MOD_ALT = &H1
  11.     MOD_CONTROL = &H2
  12.     MOD_SHIFT = &H4
  13. End Enum
  14.  
  15. Public Const WM_HOTKEY = &H312
  16.  
  17. Private m_hkCount As Long
  18.  
  19. Function HotKeyActivate(ByVal hwnd As Long, _
  20.     Modifier As ModConst, Optional KeyCode As Integer) As Long
  21.  
  22.     m_hkCount = m_hkCount + 1
  23.  
  24.     ' 0 for no success, otherwise success
  25.     HotKeyActivate = RegisterHotKey(hwnd, m_hkCount, Modifier, KeyCode)
  26.  
  27. End Function
  28.  
  29. Function HotKeyDeactivate(ByVal hwnd As Long)
  30.     Dim i As Integer
  31.     For i = 1 To m_hkCount
  32.         UnregisterHotKey hwnd, i
  33.     Next i
  34.     m_hkCount = 0
  35. End Function
Then I also have a module called modSubClass.bas with the following script.

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Private Declare Function CallWindowProcA Lib "user32" ( _
  4.     ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, _
  5.     ByVal Msg As Long, ByVal wParam As Long, _
  6.     ByVal lParam As Long) As Long
  7.  
  8. Public oldProc As Long
  9.  
  10. Public Function WndProc(ByVal hwnd As Long, ByVal uMsg As Long, _
  11.     ByVal wParam As Long, ByVal lParam As Long) As Long
  12.  
  13.     'wParam - the number of the hotkey, its identification.
  14.     'lParam - HiWord is the Modifiere e.g. Shift, Ctrl, Alt
  15.     'lParam . LoWord is the KeyCode
  16.  
  17.     WndProc = 0
  18.     If uMsg = WM_HOTKEY Then
  19.         If wParam = 1 Then
  20.             MsgBox "This is the script he runs when I press my hotkey"
  21.         End If
  22.     Else
  23.         'All other messages to the old Windowprocedure
  24.         WndProc = CallWindowProcA(oldProc, hwnd, uMsg, wParam, lParam)
  25.     End If
  26. End Function
And in my form I have the following to activate the hotkey:

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Private Declare Function SetWindowLongA Lib "user32" ( _
  4.     ByVal hwnd As Long, ByVal nIndex As Long, _
  5.     ByVal dwNewLong As Long) As Long
  6. Private Const GWL_WNDPROC = -4
  7.  
  8. Private Sub Form_Load()
  9.     oldProc = SetWindowLongA(Me.hwnd, GWL_WNDPROC, AddressOf WndProc)
  10.     HotKeyActivate Me.hwnd, MOD_ALT, Asc("S")     'hotkey 1
  11. End Sub
  12.  
  13. Private Sub Form_Unload(Cancel As Integer)
  14.     HotKeyDeactivate Me.hwnd
  15.     SetWindowLongA Me.hwnd, GWL_WNDPROC, oldProc
  16. End Sub
This example only uses ALT+S as a hotkey. When you want to add hotkeys, just make them in the form script where the other one is. And in the module modSubClass, you just make in the wndProc.

If wParam = ? Then
MsgBox "This is the script he runs when I press my hotkey"
End If

With the '?' the numer of the hotkey. This number is given by m_hkCount and the first hotkey has 1, the second has 2 and so on.

Thanks for bringing me on the right track.
Jan 9 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.