Results 1 to 13 of 13

Thread: Making Short Cuts / Actions on key presses

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    124

    Making Short Cuts / Actions on key presses

    I want to be able to add a sub that only works when a Key is pressed. Is thier a way to do this in Visual Basic?

    For augments sake when the ‘Enter’ key is pressed I want the ‘Public Sub EnterKey()’ sub to run.

    I know that its possible to add a short cut to a Menu, but i don't want a visable menu, and I cannont slect the 'Enter' Key to be the short cut.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: Making Short Cuts / Actions on key presses

    Do you want this to happen anywhere in your application or only at a certain spot or on a certain control?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    124

    Re: Making Short Cuts / Actions on key presses

    Any where and everywhere.

    [edit] However I would learn more if you where able to show me both ways [/edit]

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: Making Short Cuts / Actions on key presses

    RegisterHotKey should do the trick for you.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    124

    Re: Making Short Cuts / Actions on key presses

    Sorry, but being only a AS-level computer student I didnt understand that at all. Would you be able to give me an example of the code that will make the 'Enter' key a Register Hot Key.

    Cheers Hack.

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: Making Short Cuts / Actions on key presses

    Quote Originally Posted by ichar
    Sorry, but being only a AS-level computer student I didnt understand that at all. Would you be able to give me an example of the code that will make the 'Enter' key a Register Hot Key.

    Cheers Hack.
    There is an example provided there. It uses the F key. Simply subsitute vbKeyF with vbKeyReturn. However, I should strongly suggest NOT using the Enter key has your HotKey. Enter is hit quite often in an application and it might get annoying to have your sub kick off everytime. Perhaps a function key would be better. The keycodes for function keys are vbKeyF1 through vbKeyF16

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    124

    Re: Making Short Cuts / Actions on key presses

    I could be being dumb, but I couldn't manage to find the example. If its following the link 'example' then I can't click it as my computer can't open any Internet Pages that pop-up I have to "Copy - Short Cut" and paste it into my addy bar but when I do it links to the same page.

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: Making Short Cuts / Actions on key presses

    Here is the example
    VB Code:
    1. Private Const MOD_ALT = &H1
    2. Private Const MOD_CONTROL = &H2
    3. Private Const MOD_SHIFT = &H4
    4. Private Const PM_REMOVE = &H1
    5. Private Const WM_HOTKEY = &H312
    6. Private Type POINTAPI
    7.     x As Long
    8.     y As Long
    9. End Type
    10. Private Type Msg
    11.     hWnd As Long
    12.     Message As Long
    13.     wParam As Long
    14.     lParam As Long
    15.     time As Long
    16.     pt As POINTAPI
    17. End Type
    18. Private Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
    19. Private Declare Function UnregisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long) As Long
    20. 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
    21. Private Declare Function WaitMessage Lib "user32" () As Long
    22. Private bCancel As Boolean
    23. Private Sub ProcessMessages()
    24.     Dim Message As Msg
    25.     'loop until bCancel is set to True
    26.     Do While Not bCancel
    27.         'wait for a message
    28.         WaitMessage
    29.         'check if it's a HOTKEY-message
    30.         If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
    31.             'minimize the form
    32.             WindowState = vbMinimized
    33.         End If
    34.         'let the operating system process other events
    35.         DoEvents
    36.     Loop
    37. End Sub
    38. Private Sub Form_Load()
    39.     'KPD-Team 2000
    40.     'URL: [url]http://www.allapi.net/[/url]
    41.     'E-Mail: [email]KPDTeam@Allapi.net[/email]
    42.     Dim ret As Long
    43.     bCancel = False
    44.     'register the Ctrl-F hotkey
    45.     ret = RegisterHotKey(Me.hWnd, &HBFFF&, MOD_CONTROL, vbKeyF)
    46.     'show some information
    47.     Me.AutoRedraw = True
    48.     Me.Print "Press CTRL-F to minimize this form"
    49.     'show the form and
    50.     Show
    51.     'process the Hotkey messages
    52.     ProcessMessages
    53. End Sub
    54. Private Sub Form_Unload(Cancel As Integer)
    55.     bCancel = True
    56.     'unregister hotkey
    57.     Call UnregisterHotKey(Me.hWnd, &HBFFF&)
    58. End Sub

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    124

    Re: Making Short Cuts / Actions on key presses

    when i run the code, i get the error
    Code:
    Argument not optional
    And the pointer points to
    VB Code:
    1. Private Sub Form_Load()
    2.     'KPD-Team 2000
    3.     'URL: [url]http://www.allapi.net/[/url]
    4.     'E-Mail: [email]KPDTeam@Allapi.net[/email]
    5.     Dim ret As Long
    6.     bCancel = False
    7.     'register the Return hotkey
    8.     [b]ret = RegisterHotKey(Me.hWnd, &HBFFF&, , vbKeyReturn)[/b] ==points here==
    9.     'show some information
    10.     Me.AutoRedraw = True
    11.     Me.Print "Press CTRL-F to minimize this form"
    12.     'show the form and
    13.     Show
    14.     'process the Hotkey messages
    15.     ProcessMessages
    16. End Sub

    Any ideas?

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: Making Short Cuts / Actions on key presses

    The example passes 4 parameters to the RegisterHotKey API
    VB Code:
    1. ret = RegisterHotKey(Me.hWnd, &HBFFF&, MOD_CONTROL, vbKeyF)
    You are only passing three
    VB Code:
    1. ret = RegisterHotKey(Me.hWnd, &HBFFF&, , vbKeyReturn)
    The third parameter can't be empty.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    124

    Re: Making Short Cuts / Actions on key presses

    Thats becuase i don't want to set it too Ctrl+Enter, but only enter. Is thier a null vaule i could put in it to make it work?

  12. #12
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: Making Short Cuts / Actions on key presses

    Quote Originally Posted by ichar
    Thats becuase i don't want to set it too Ctrl+Enter, but only enter. Is thier a null vaule i could put in it to make it work?
    Enter, by itself, should be left to do what Enter always does. Making a key like Enter do something unexpected is not good programming practice, and will more than likely confuse the heck out of your users. Ctrl+Enter would be a good combination.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    124

    Re: Making Short Cuts / Actions on key presses

    I really do want it to be just enter tho.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width