Results 1 to 6 of 6

Thread: How can ignore the specific key

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Location
    Thai
    Posts
    13

    How can ignore the specific key

    such as start up key(keycode = 91) .
    Thank you very much for every reply

  2. #2
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048

    Re: How can ignore the specific key

    what is a start up key?

    and what do you want to ignore the key, your program, the system?

  3. #3
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: How can ignore the specific key

    Quote Originally Posted by Sung007
    such as start up key(keycode = 91) .
    Thank you very much for every reply
    If you mean the key with the windows logo, the key that brings up start menu,
    it could also be fired pressing Ctrl- Esc.
    About ingoring this sort of key, take a look Here

    Many other common keys can be ignored like this:
    VB Code:
    1. Private Sub Form_KeyPress(KeyAscii As Integer)
    2.     If KeyAscii = Asc("a") Or KeyAscii = Asc("A") Then KeyAscii = 0
    3. End Sub
    4.  
    5. Private Sub Form_Load()
    6.     Me.KeyPreview = True
    7. End Sub

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How can ignore the specific key

    You can use a low-level keyboard hook to discard the Window key (doesn't work on Win9x/ME). Add the following code to a regular BAS module.
    VB Code:
    1. Private Declare Function SetWindowsHookEx _
    2.  Lib "user32" Alias "SetWindowsHookExA" ( _
    3.  ByVal idHook As Long, _
    4.  ByVal lpfn As Long, _
    5.  ByVal hmod As Long, _
    6.  ByVal dwThreadId As Long) As Long
    7.  
    8. Private Declare Function CallNextHookEx Lib "user32" ( _
    9.  ByVal hHook As Long, _
    10.  ByVal nCode As Long, _
    11.  ByVal wParam As Long, _
    12.  ByVal lParam As Long) As Long
    13.  
    14. Private Declare Function UnhookWindowsHookEx Lib "user32" ( _
    15.  ByVal hHook As Long) As Long
    16.  
    17. Private Declare Sub CopyMemory _
    18.  Lib "kernel32" Alias "RtlMoveMemory" ( _
    19.  pDest As Any, _
    20.  pSource As Any, _
    21.  ByVal cb As Long)
    22.  
    23. Private Type KBDLLHOOKSTRUCT
    24.     vkCode As Long
    25.     scanCode As Long
    26.     flags As Long
    27.     time As Long
    28.     dwExtraInfo As Long
    29. End Type
    30.  
    31. Private Const HC_ACTION = 0&
    32. Private Const WH_KEYBOARD_LL = 13&
    33. Private Const VK_LWIN = &H5B&
    34. Private Const VK_RWIN = &H5C&
    35.  
    36. Private hKeyb As Long
    37.  
    38. Private Function KeybCallback(ByVal Code As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    39.     Static udtHook As KBDLLHOOKSTRUCT
    40.    
    41.     If (Code = HC_ACTION) Then
    42.         'Copy the keyboard data out of the lParam (which is a pointer)
    43.         Call CopyMemory(udtHook, ByVal lParam, Len(udtHook))
    44.         Select Case udtHook.vkCode
    45.             Case VK_LWIN, VK_RWIN
    46.                 KeybCallback = 1
    47.                 Exit Function
    48.         End Select
    49.     End If
    50.     KeybCallback = CallNextHookEx(hKeyb, Code, wParam, lParam)
    51. End Function
    52.  
    53. Public Sub HookKeyboard()
    54.     UnhookKeyboard
    55.     hKeyb = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf KeybCallback, App.hInstance, 0&)
    56. End Sub
    57.  
    58. Public Sub UnhookKeyboard()
    59.     If hKeyb <> 0 Then
    60.         Call UnhookWindowsHookEx(hKeyb)
    61.         hKeyb = 0
    62.     End If
    63. End Sub
    In Form_Load call HookKeyboard and in Form_Unload call the UnhookKeyboard sub.

  5. #5
    New Member
    Join Date
    Jun 2012
    Posts
    1

    Re: How can ignore the specific key

    Quote Originally Posted by Joacim Andersson View Post
    You can use a low-level keyboard hook to discard the Window key (doesn't work on Win9x/ME). Add the following code to a regular BAS module.
    VB Code:
    1. Private Declare Function SetWindowsHookEx _
    2.  Lib "user32" Alias "SetWindowsHookExA" ( _
    3.  ByVal idHook As Long, _
    4.  ByVal lpfn As Long, _
    5.  ByVal hmod As Long, _
    6.  ByVal dwThreadId As Long) As Long
    7.  
    8. Private Declare Function CallNextHookEx Lib "user32" ( _
    9.  ByVal hHook As Long, _
    10.  ByVal nCode As Long, _
    11.  ByVal wParam As Long, _
    12.  ByVal lParam As Long) As Long
    13.  
    14. Private Declare Function UnhookWindowsHookEx Lib "user32" ( _
    15.  ByVal hHook As Long) As Long
    16.  
    17. Private Declare Sub CopyMemory _
    18.  Lib "kernel32" Alias "RtlMoveMemory" ( _
    19.  pDest As Any, _
    20.  pSource As Any, _
    21.  ByVal cb As Long)
    22.  
    23. Private Type KBDLLHOOKSTRUCT
    24.     vkCode As Long
    25.     scanCode As Long
    26.     flags As Long
    27.     time As Long
    28.     dwExtraInfo As Long
    29. End Type
    30.  
    31. Private Const HC_ACTION = 0&
    32. Private Const WH_KEYBOARD_LL = 13&
    33. Private Const VK_LWIN = &H5B&
    34. Private Const VK_RWIN = &H5C&
    35.  
    36. Private hKeyb As Long
    37.  
    38. Private Function KeybCallback(ByVal Code As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    39.     Static udtHook As KBDLLHOOKSTRUCT
    40.    
    41.     If (Code = HC_ACTION) Then
    42.         'Copy the keyboard data out of the lParam (which is a pointer)
    43.         Call CopyMemory(udtHook, ByVal lParam, Len(udtHook))
    44.         Select Case udtHook.vkCode
    45.             Case VK_LWIN, VK_RWIN
    46.                 KeybCallback = 1
    47.                 Exit Function
    48.         End Select
    49.     End If
    50.     KeybCallback = CallNextHookEx(hKeyb, Code, wParam, lParam)
    51. End Function
    52.  
    53. Public Sub HookKeyboard()
    54.     UnhookKeyboard
    55.     hKeyb = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf KeybCallback, App.hInstance, 0&)
    56. End Sub
    57.  
    58. Public Sub UnhookKeyboard()
    59.     If hKeyb <> 0 Then
    60.         Call UnhookWindowsHookEx(hKeyb)
    61.         hKeyb = 0
    62.     End If
    63. End Sub
    In Form_Load call HookKeyboard and in Form_Unload call the UnhookKeyboard sub.
    The AddressOf operator doesn't working.
    Please resolve it.
    Thank you.

  6. #6
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: How can ignore the specific key

    Quote Originally Posted by scsoumya View Post
    The AddressOf operator doesn't working.
    Please resolve it.
    Thank you.
    Hi... Welcome to the forums

    Please note that you were replying to a 6 year old thread!

    I just tested Joacim Andersson's code and it worked fine in my Windows XP machine.
    If you got any issues while testing that code, then create a new thread in here specifying the details.

    Also note that, this is VB6 code. So, you have to copy that code in a new Module in your project and inside a form, you need to call the subs HookKeyboard (to hook it) and UnhookKeyboard (unhooks)

    Hope this helps.

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

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