Consulting

Results 1 to 16 of 16

Thread: Disable Ctrl key actions

  1. #1
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    271
    Location

    Disable Ctrl key actions

    Is it possible to disable the Ctrl key functions?
    I have disabled the toolbars but using the Ctrl and "others" keys still allows the user to make amendments and saves etc
    eg Ctrl + S Ctrl + B

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Yes, you can create do-nothing macros and point the keys at your macros

    [vba]

    Sub doNothing()
    End Sub

    'set it somewhere
    Application.OnKey "^{c}", "doNothing"
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    271
    Location
    Quote Originally Posted by xld
    Yes, you can create do-nothing macros and point the keys at your macros

    [vba]

    Sub doNothing()
    End Sub

    'set it somewhere
    Application.OnKey "^{c}", "doNothing"
    [/vba]
    Got around to looking at this but am still having issues
    I put the code;
    [vba]Application.OnKey "^{c}", "doNothing"[/vba]
    in the "workbook_open()" code

    if the sub "donothing" does not exist then pressing ctrl & S brings up the message:
    macro "doNothing" does not exist which shows using the ctrl key is looking for the sub
    and nothing happens, which is what I want but without the message

    When the "doNothing" macro exists using ctrl & S the workbook saves which is what I was trying to prevent.

    Should the code in the workbook open be something like:

    [vba]Application.OnKey "^{c}", & 'any other key' "doNothing"[/vba]

  4. #4
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    You need to use OnKey for each combination you want to block - so:
    [VBA]application.OnKey "^c", ""
    application.OnKey "^S", ""
    application.OnKey "^U", ""
    ...etc.[/VBA]
    Regards,
    Rory

    Microsoft MVP - Excel

  5. #5
    Sometimes when you drag a horse to water it says "I only drink Evian" !
    2+2=9 ... (My Arithmetic Is Mental)

  6. #6
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    271
    Location
    Only asking and trying to learn

  7. #7
    Go on I'll bite, but I'll probably regret it ...

    Why did you think putting code in to intercept "Ctrl-c" would do ANYTHING when the user used "Ctrl-s" ???
    2+2=9 ... (My Arithmetic Is Mental)

  8. #8
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    271
    Location
    In XLDs first response
    [vba]Application.OnKey "^{c}", "doNothing" [/vba]
    I assumed ^{c} was the code for the ctrl key and not Ctrl + C and just disabling the Ctrl key would also disable ctrl and any other key aswell

    I know have learned that just the ^ indicates the ctrl key

  9. #9
    Okay, ... I accept that, ... you have my humble appology ...
    2+2=9 ... (My Arithmetic Is Mental)

  10. #10
    VBAX Regular Darren's Avatar
    Joined
    Feb 2005
    Posts
    98
    Location
    Hi captains

    Just a quick question on the above post about disabling Ctrl keys

    [VBA]Application.OnKey "^{c}", "doNothing"[/VBA]

    How would i apply this to the Tab Key on the keyboard ?

    Thanks for you help\
    Darren
    South Africa
    Live by Ghandi and learn 1st by "Vbaexpress.com and then by Google" !!!

  11. #11
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    You would use:
    [vba]application.OnKey "{tab}", ""
    application.OnKey "+{tab}", ""[/vba]

    If you don't add the second one, they can Shift+tab backwards but not tab forwards.
    Regards,
    Rory

    Microsoft MVP - Excel

  12. #12
    VBAX Regular Darren's Avatar
    Joined
    Feb 2005
    Posts
    98
    Location
    Hi Rory

    That was quick Thanks Champion

    Darren
    Live by Ghandi and learn 1st by "Vbaexpress.com and then by Google" !!!

  13. #13
    VBAX Regular Darren's Avatar
    Joined
    Feb 2005
    Posts
    98
    Location
    Hi Rory

    i entered the code as follows but its still active
    [VBA]Option Explicit
    Private Sub UserForm_Activate()
    TextBox1 = 1#
    TextBox2 = 0#
    txtTurnoverIncl.SetFocus
    Application.OnKey "+{tab}", ""
    End Sub
    Private Sub UserForm_Initialize()
    Dim i As Long
    For i = 0 To 35
    cmbRoyalty.AddItem Format(i / 100, "0%")
    Next i
    Application.OnKey "+{tab}", ""
    TextBox1.Value = 1#
    txtTurnoverIncl = TextBox1
    TextBox2.Value = 0
    cmbRoyalty.Text = TextBox2
    txtTurnoverIncl.SetFocus

    End Sub[/VBA]

    Your thoughts
    Darren
    Live by Ghandi and learn 1st by "Vbaexpress.com and then by Google" !!!

  14. #14
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I think OnKey is inactive whilst a form is active. But why would you want to disable tab on a form?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  15. #15
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    My thoughts are many and varied
    1. You didn't mention a userform before. OnKey does not affect userforms.
    2. Why would you want to disable the tab key?
    3. Set the TabStop to false for all controls on the form or use the KeyUp event for every control.
    Regards,
    Rory

    Microsoft MVP - Excel

  16. #16
    VBAX Regular Darren's Avatar
    Joined
    Feb 2005
    Posts
    98
    Location
    Hi Rory

    Sorry i did not mention Userform.

    I had chose enter as a navigation key in the useform, Its seems to be a bad choice. looking at tab it seems more user friendly with properties down the left and tabindex set properly works fine.

    I have small problem that causes Type Mismatch 13 errors, so i chose enter and backspace as the drivers in the user form. if i enter a number value in userform A say of 2000 in TextBox1 i then use enter and got to Userform B enter a value in TextBox1 on that Userform all is perfect, This is the crunch when i go back to userformA and change the value using delete after 3rd press of delete i get Type Mismatch 13 error. However if i use backspace all is well and no error ?

    To summarize i wanted to only use ENTER and BACKSPACE as navigation tools.... so i tried to make TAB and DELETE keys inactive on the userform.

    I will use TAB to navigate from now on but if i stop the delete causing error would solve the problem.

    I trust my intention is better understood.

    Darren
    South Africa
    Live by Ghandi and learn 1st by "Vbaexpress.com and then by Google" !!!

Posting Permissions

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