Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

Create shortcut keys

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
nhariharann
Asked:
nhariharann
1 Solution
 
deightonCommented:
'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
 
deightonCommented:
..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
 
egsemsemCommented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
iboutchkineCommented:
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
 
DanRollinsCommented:
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
 
Computer101Commented:
Comment from expert accepted as answer

Computer101
E-E Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now