Results 1 to 16 of 16

Thread: Killing Keys When Pressed

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    Virginia
    Posts
    55

    Unhappy

    I was told that this would work to kill the processing of a tab key when it is pressed:

    private sub text1_KeyDown (shift as integer, keycode as integer)
    if keycode = 9 then 'tab key
    keycode = 0
    end if
    end sub

    It does not work however. Is there any way to kill it without sending a backspace or anything like that? I'm trying to make my tabs in a multiline text box 4 spaces instead of 8, but the tab is still sent after I send the four spaces. Any help will be rewarded with profuse thanks.
    Thanks,
    Chris

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'You can set the Tabstop property to false for all the controls: 
    'disable the tab form
    
    Private Sub Form_Load() 
    	Dim i As Integer 
    	On Error Resume Next 
    	For i = 0 To Controls.Count - 1   ' Use the Controls collection 
       		Controls(i).TabStop = False 
    	Next 
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    Virginia
    Posts
    55

    Unhappy Not Quite what I'm asking...

    I don't want to kill the tabstop, just the processing of a tab in the textbox. Tab doesn't change controls as it is, but it does insert 8 spaces. OnKeyDown, I would like to kill the tab and insert 4 spaces instead but I don't know how to kill the tab after I have captured it or any key for that matter. A similar example would be inserting "b" when the "a" key is pressed without sending a backspace to delete the a. I don't want the "a" to appear at all.
    Thanks,
    Chris

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    try returning the focus to the textbox with textboxname.setfocus
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    Virginia
    Posts
    55

    Wink Still not it...

    Apparently more background is needed. My main form is one huge textbox used for text editing. When the person hits tab at this point, 8 spaces are entered into the textbox. I would like to have a code that detects the tab and puts four spaces instead of 8. This is the code I used:

    private sub text1_KeyDown(KeyCode as integer, Shift as Integer)

    if keycode = vbKeyTab then
    Text1.seltext = " "
    KeyCode = 0
    End if

    Unfortunately, when the user pressed tab, it inserted my four spaces but then also inserted a tab (in the text box). I thought that the "keycode = 0" would cancel the tab so that only four spaces would appear, but I was wrong. Is there a way to do this?
    Thanks,
    Chris

  6. #6
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    I think you could use...

    ...the Text1_KeyPress Event

    Code:
    If KeyAscii = vbKeyTab Then
    KeyAscii = 0
    'MsgBox "TAB"
    'Code for 4 spaces
    End If
    Did that help for ya?
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  7. #7

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    Virginia
    Posts
    55

    Unhappy Doesn't seem to work for me.

    I tried this just to test:

    Private Sub form_KeyAscii(KeyAscii As Integer, Shift As Integer)
    If KeyAscii = vbKeyS Then
    KeyAscii = 0
    Text1.SelText = "a"
    End If
    End Sub

    And the output is "as" every time I hit the s key. How come it's not cancelling the "s"?
    Thanks,
    Chris

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    Virginia
    Posts
    55

    Wink Correction...

    I meant:

    Private Sub Form_KeyDown(...
    ...
    Thanks,
    Chris

  9. #9
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    What?

    Private Sub form_KeyAscii(KeyAscii As Integer, Shift As Integer)
    Did you copy & paste it? or did you just typed it?

    Maybe that's the prob hehe
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  10. #10

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    Virginia
    Posts
    55

    Talking Oops...

    The error was in the retype, not the original. They need an embarrassed face in the icon selection.

    In my application I typed "text1_KeyDown"
    Thanks,
    Chris

  11. #11
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    ok, KeyAscii means you have to enter the ASCII number for the number, so: S = 115
    So use:
    Code:
    If KeyAscii = 115 Then
    KeyAscii = 0
    Text1.SelText = "a"
    End If
    That'll work

    Please let me know if it works


    [Edited by Jop on 09-07-2000 at 11:43 AM]
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  12. #12

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    Virginia
    Posts
    55

    Unhappy Still a bug...

    It turns out the Ascii value for s is 83 so I tried that instead as you suggested. I still get "as" returned. I really appreciate the time you are taking to try to help me out. This is incredibly frustrating.
    Thanks,
    Chris

  13. #13
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    hmm the UCase(s) = 83 yes

    but what isn't working then?
    Please give me the code you're using... Wait I'll test it for ya one more time...

    ok when I do:
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 83 Then
    KeyAscii = 0
    Text1.SelText = "a"
    End If
    End Sub
    It just adds a to the text that's already in the box!
    Damn I don't get it
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  14. #14
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    hmm the UCase(s) = 83 yes

    but what isn't working then?
    Please give me the code you're using... Wait I'll test it for ya one more time...

    ok when I do:
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 83 Then
    KeyAscii = 0
    Text1.SelText = "a"
    End If
    End Sub
    It works for me when I press <Shift> + s
    It just adds a to the text that's already in the box!
    Damn I don't get it why it won't work for you... please test it with this code.
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  15. #15

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    Virginia
    Posts
    55

    Talking Halleluia!!!

    It's the difference between KeyDown and KeyPress apparently. When I copied and pasted your code it worked for me also. Thank you, Thank you, Thank you. Your name will go into the credits.
    Thanks,
    Chris

  16. #16
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Thank you, Thank you, Thank you. Your name will go into the credits
    I would love it (you to put my name in the credits) but it's not neccesary, but again, no problem if you do

    also, you can add me via ICQ (18818940) if ya want!

    BTW, what does the prog?

    [Edited by Jop on 09-07-2000 at 12:24 PM]
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

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