Results 1 to 12 of 12

Thread: Enable & Disable Function For Keys

  1. #1

    Thread Starter
    Lively Member kshadow22's Avatar
    Join Date
    Dec 2014
    Location
    Kentucky
    Posts
    94

    Enable & Disable Function For Keys

    I need some coding which will allow me to disable a button, such as CTRL. I will need to be able to enable CTRL after. Basically, I will need a on/off function which will disable the key. I have found a code that works Great! But it doesn't allow me to enable the button until the App closes out. I am programming in Visual Basic 2008. Any Visual Basic code will do tbh!

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,449

    Re: Enable & Disable Function For Keys

    Do you actually mean a key rather than a button, as in on the physical keyboard? Are you talking about disabling that key system-wide or just within your application? If it's the latter, why would you want to do something like disable the Ctrl key system-wide?

  3. #3

    Thread Starter
    Lively Member kshadow22's Avatar
    Join Date
    Dec 2014
    Location
    Kentucky
    Posts
    94

    Re: Enable & Disable Function For Keys

    Quote Originally Posted by jmcilhinney View Post
    Do you actually mean a key rather than a button, as in on the physical keyboard? Are you talking about disabling that key system-wide or just within your application? If it's the latter, why would you want to do something like disable the Ctrl key system-wide?
    I would need it System-Wide. It would need to be for the keys as well. I have done this before, except I couldn't turn it off when I wanted to. I would have to exit the whole application to turn it off. I just need a code that acts like a on and off switch.

  4. #4
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,390

    Re: Enable & Disable Function For Keys

    do you not think posting your code might help us?

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,241

    Re: Enable & Disable Function For Keys

    I agree. You sound like you have it half way, which means that you are probably REALLY close to having it right. Show us what you did and we can probably suggest the rest.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Lively Member kshadow22's Avatar
    Join Date
    Dec 2014
    Location
    Kentucky
    Posts
    94

    Re: Enable & Disable Function For Keys

    Here is the Code I am using as of now:
    Option Strict On
    Imports System.Runtime.InteropServices




    Private Const KEYEVENTF_EXTENDEDKEY As Long = &H1
    Private Const KEYEVENTF_KEYUP As Long = &H2
    Private Const VK_LWIN As Byte = &H5B
    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
    ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

    Private Const WH_KEYBOARD_LL As Integer = 13
    Private Const WM_KEYUP As Integer = &H101
    Private Shared _proc As LowLevelKeyboardProc = AddressOf HookCallback
    Private Shared _hookID As IntPtr = IntPtr.Zero

    Public Declare Auto Function SetWindowsHookEx Lib "user32.dll" ( _
    ByVal idHook As Integer, ByVal lpfn As LowLevelKeyboardProc, _
    ByVal hMod As IntPtr, ByVal dwThreadId As UInteger) As IntPtr

    Public Declare Auto Function UnhookWindowsHookEx _
    Lib "user32.dll" (ByVal hhk As IntPtr) As IntPtr

    Public Declare Auto Function CallNextHookEx _
    Lib "user32.dll" (ByVal hhk As IntPtr, ByVal nCode As Integer, _
    ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr

    Public Declare Auto Function GetModuleHandle Lib "kernel32.dll" ( _
    ByVal lpModuleName As String) As IntPtr


    Private Shared Function SetHook( _
    ByVal proc As LowLevelKeyboardProc) As IntPtr

    Dim curProcess As Process = Process.GetCurrentProcess()
    Dim curModule As ProcessModule = curProcess.MainModule

    Return SetWindowsHookEx(WH_KEYBOARD_LL, proc, _
    GetModuleHandle(curModule.ModuleName), 0)

    End Function

    Public Delegate Function LowLevelKeyboardProc( _
    ByVal nCode As Integer, ByVal wParam As IntPtr, _
    ByVal lParam As IntPtr) As IntPtr

    Public Shared Function HookCallback( _
    ByVal nCode As Integer, _
    ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr

    If nCode >= 0 And wParam = CType(WM_KEYUP, IntPtr) Then
    Dim vkCode As Keys = CType(Marshal.ReadInt32(lParam), Keys)
    If vkCode = Keys.LWin Or vkCode = Keys.RWin Then
    keybd_event(CByte(Keys.Zoom), 0, KEYEVENTF_EXTENDEDKEY, 0)
    End If
    End If

    Return CallNextHookEx(_hookID, nCode, wParam, lParam)
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    _hookID = SetHook(_proc)
    End Sub

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
    UnhookWindowsHookEx(_hookID)
    End Sub

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,241

    Re: Enable & Disable Function For Keys

    So...you have all the code. Is the entire problem that you unhook in the closed event and you want to be able to do it somewhere else?
    My usual boring signature: Nothing

  8. #8
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,390

    Re: Enable & Disable Function For Keys

    its already there!!!!

  9. #9

    Thread Starter
    Lively Member kshadow22's Avatar
    Join Date
    Dec 2014
    Location
    Kentucky
    Posts
    94

    Re: Enable & Disable Function For Keys

    It works great. But after the function is activated, I can't deactivate if that makes sense. The only way I can seem to deactivate it is to exit the whole application. I need a code that will allow me to deactivate the code while still in the application

  10. #10
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,623

    Re: Enable & Disable Function For Keys

    What people are trying to tell you is you already have the code.

    This bit -

    Code:
    UnhookWindowsHookEx(_hookID)
    Which you have in the Form Close Event

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
    UnhookWindowsHookEx(_hookID)
    End Sub
    You just need to call it from somewhere else.

    You could create a button for instance and add the UnhookWindowsHookEx(_hookID) call to the buttons Click event
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,954

    Re: Enable & Disable Function For Keys

    That said, it would probably still be a good idea to keep it in the form unload event, as it would provide a safety catch in case you forget to "unlock" your keys before exiting the program.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12

    Thread Starter
    Lively Member kshadow22's Avatar
    Join Date
    Dec 2014
    Location
    Kentucky
    Posts
    94

    Re: Enable & Disable Function For Keys

    Oh gosh. Thank you so much!!!!!! I guess I should have looked into the code a bit more. thank you so much

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