Solved

Create shortcut keys

Posted on 2001-07-12
6
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

752 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