Solved

Create shortcut keys

Posted on 2001-07-12
6
218 Views
Last Modified: 2007-12-19
How can create shortcut keys in vb ?
when we press combine keys like Ctrl + A  (or) Alt + Ctrl + A ... like this is the following event not working properly.

Private Sub Form_KeyDown(KeyCode As Integer, _
      Shift As Integer)
   ShiftKey = Shift And 7
   Select Case ShiftKey
      Case 1 ' or vbShiftMask
         MsgBox "You pressed the SHIFT key."
      Case 2 ' or vbCtrlMask
         MsgBox "You pressed the CTRL key."
      Case 4 ' or vbAltMask
         MsgBox "You pressed the ALT key."
      Case 3
         MsgBox "You pressed both SHIFT and CTRL."
      Case 5
         MsgBox "You pressed both SHIFT and ALT."
      Case 6
         MsgBox "You pressed both CTRL and ALT."
      Case 7
         MsgBox "You pressed SHIFT, CTRL, and ALT."
      End Select
End Sub

 Is it any keywords are available in VB ? Like foxpro and
visual foxpro we can access the keys like this {Ctrl+Alt} , {Ctrl+LeftArrow}.
Is it any commands available like onkeylabel (visual foxpro and foxpro) in visual basic.

How can i trap the combined key's through my programme.

regards
Harish.





0
Comment
Question by:nhariharann
6 Comments
 
LVL 18

Accepted Solution

by:
deighton earned 100 total points
ID: 6276537
'have you got

form.keypreview = true (in design mode)

Private Sub Form_KeyDown(KeyCode As Integer, _
     Shift As Integer)
     
     Dim shiftkey As Long
     
  shiftkey = Shift And 7
  Select Case shiftkey
     Case 1 ' or vbShiftMask
        MsgBox "You pressed the SHIFT key."
     Case 2 ' or vbCtrlMask
        MsgBox "You pressed the CTRL key."
     Case 4 ' or vbAltMask
        MsgBox "You pressed the ALT key."
     Case 3
        MsgBox "You pressed both SHIFT and CTRL."
     Case 5
        MsgBox "You pressed both SHIFT and ALT."
     Case 6
        MsgBox "You pressed both CTRL and ALT."
     Case 7
        MsgBox "You pressed SHIFT, CTRL, and ALT."
     End Select
End Sub



0
 
LVL 18

Expert Comment

by:deighton
ID: 6276554
..also if you want to monitor for two keys pressed at once, you realise that you cant pop up the message box as soon as the first goes down, so if you do something like that when cntrl or alt is pressed, you will never get to cntrl - alt combination

ok this works for control alt b for example

Private Sub Form_KeyDown(KeyCode As Integer, _
     Shift As Integer)
     
     Dim shiftkey As Long
     
  shiftkey = Shift And 7
  Select Case shiftkey
  '   Case 1 ' or vbShiftMask
       ' MsgBox "You pressed the SHIFT key."
   '  Case 2 ' or vbCtrlMask
        'MsgBox "You pressed the CTRL key."
    ' Case 4 ' or vbAltMask
        'MsgBox "You pressed the ALT key."
     'Case 3
      '  MsgBox "You pressed both SHIFT and CTRL."
     'Case 5
      '  MsgBox "You pressed both SHIFT and ALT."
     Case 6
        Debug.Print KeyCode
        If KeyCode = vbKeyB Then
            MsgBox "You pressed both CTRL and ALT B"
           
        End If
     Case 7
        MsgBox "You pressed SHIFT, CTRL, and ALT."
     End Select
End Sub



Private Sub Form_Load()
Me.KeyPreview = True
End Sub
0
 
LVL 2

Expert Comment

by:egsemsem
ID: 6276725
Try this code (from ApiGuide):

Private Const MOD_ALT = &H1
Private Const MOD_CONTROL = &H2
Private Const MOD_SHIFT = &H4
Private Const PM_REMOVE = &H1
Private Const WM_HOTKEY = &H312
Private Type POINTAPI
    x As Long
    y As Long
End Type
Private Type Msg
    hWnd As Long
    Message As Long
    wParam As Long
    lParam As Long
    time As Long
    pt As POINTAPI
End Type
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 PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
Private Declare Function WaitMessage Lib "user32" () As Long
Private bCancel As Boolean
Private Sub ProcessMessages()
    Dim Message As Msg
    'loop until bCancel is set to True
    Do While Not bCancel
        'wait for a message
        WaitMessage
        'check if it's a HOTKEY-message
        If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
            'minimize the form
            WindowState = vbMinimized
        End If
        'let the operating system process other events
        DoEvents
    Loop
End Sub
Private Sub Form_Load()
    'KPD-Team 2000
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    Dim ret As Long
    bCancel = False
    'register the Ctrl-F hotkey
    ret = RegisterHotKey(Me.hWnd, &HBFFF&, MOD_CONTROL, vbKeyF)
    'show some information
    Me.AutoRedraw = True
    Me.Print "Press CTRL-F to minimize this form"
    'show the form and
    Show
    'process the Hotkey messages
    ProcessMessages
End Sub
Private Sub Form_Unload(Cancel As Integer)
    bCancel = True
    'unregister hotkey
    Call UnregisterHotKey(Me.hWnd, &HBFFF&)
End Sub

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 28

Expert Comment

by:iboutchkine
ID: 6276793
Try this

Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long

Private Sub Form_Load()
Dim keybs(0 To 255) As Byte
Me.Show

Do While
DoEvents
GetKeyboardState keybs(0)
For x = 0 To 255
If keybs(16) And 128 And keybs(17) And 128 And keybs(65) And 128 Then
Me.SetFocus
Me.WindowState = vbNormal
End If
Next x

Loop

Unload Me

End Sub

this code popups you application when ctrl + shift + a pressed
keybs(17)=ctrl keybs(16)=shift keybs(65)=a
you can replace and modify all kind of combinations as you like.
You can find the vbkey constants in you visual basic help.


0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7153061
Hi nhariharann,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept deighton's comment(s) as an answer.

nhariharann, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 
LVL 1

Expert Comment

by:Computer101
ID: 7182202
Comment from expert accepted as answer

Computer101
E-E Moderator
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

705 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

12 Experts available now in Live!

Get 1:1 Help Now