Results 1 to 6 of 6

Thread: Probably basic...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Iowa
    Posts
    96

    Probably basic...

    I'm trying to make my program execute "stuff" when I press a key such as F1 (or any other key I choose) I want to do this WITHOUT shortcuts, so that i can later do it without having an object heres what I have so far


    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2. 'adds the handler and sets address only works when it's in the text box
    3.         AddHandler txtmacro.KeyPress, AddressOf key
    4.     End Sub
    5. Public Sub key(ByVal o As System.Object, ByVal e As KeyPressEventArgs)
    6. 'Checks to see when the letter "a" is pressed and then does "stuff"
    7. If e.KeyChar = Microsoft.VisualBasic.ChrW(97) Then        
    8. Stuff
    9.  
    10.         End If
    11. End Sub

    Now this works fine under 2 condittions
    1)I'm typing in my text box
    2)I use a key that has a common ascii code such as a-z A-Z or 0-9

    When i put in "&H6F" for the key (which I thought was the F1 key) It doesnt give me any errors but it doesnt do the "stuff"

    Also I would like for this to work at any time, and not just when the cursor is in the text box.

    Any help or suggestions would be greatly appreciated
    even if you have a completely differant way of doing this, as long as it works that's great.

    Any questions please ask!!
    It is like wiping your ass with silk, I love it!

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Do you want your form to trap all keys first? even if you are in a Textbox or other control? If so you should set KeyPreview of your form to True, then:
    VB Code:
    1. Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    2.         If  e.KeyCode = Keys.F1 Then
    3.             ' Do the stuff here
    4.         End If
    5.         e.Handled = True
    6. End Sub
    Last edited by Lunatic3; Feb 14th, 2004 at 12:44 AM.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  3. #3
    Hyperactive Member ZeroCool's Avatar
    Join Date
    Feb 2002
    Location
    In front of my computer
    Posts
    423
    Checkout Knowlege Base Article: KB320583

    I attached a sample that involves creating a user control that inherits from a textbox

    it provides two new events SpecialKeyUp and SpecialKeyDown
    these events will fire when the control receives a WM_KEYUP, WM_KEYDOWN, WM_SYSKEYUP, WM_SYSKEYDOWN

    Both events are declared like this

    byval msg as Message, byval key as Keys

    msg will be WM_KEYUP, WM_KEYDOWN, WM_SYSKEYUP or WM_SYSKEYDOWN

    key will be the key that was pressed
    Attached Files Attached Files
    Last edited by ZeroCool; Feb 14th, 2004 at 12:46 AM.
    Visual Baisc 6 (SP5)
    Windows Xp

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Iowa
    Posts
    96
    Lunatic Thanks what you said works

    Zero, I don't entirely understand what you said, (i'm very beginner) If you would like you can try to explain it better. Thanks !!
    It is like wiping your ass with silk, I love it!

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Iowa
    Posts
    96
    What if i want to do 2 keys such as

    Shift+F1

    I dont really know what i'm doing but i tried a couple things, and it always just does the thing for F1, and never does the thing for Shift+F1
    It is like wiping your ass with silk, I love it!

  6. #6
    Hyperactive Member ZeroCool's Avatar
    Join Date
    Feb 2002
    Location
    In front of my computer
    Posts
    423
    for my example you need to download the attachment that i posted that will provide you with a simple control that inherits from the textbox, for a begginer you might be better off with Lunatic3's example.


    VB Code:
    1. 'Same thing with my control --->
    2.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3.  
    4.         AddHandler CtrlTextbox1.SpecialKeyDown, AddressOf Key_Event
    5.     End Sub
    6.  
    7.     Public Sub Key_Event(ByVal msg As Message, ByVal key As Keys)
    8.         If key = (Keys.F1 Or Keys.Shift) Then
    9.             SumFunction()
    10.         End If
    11.     End Sub
    12.  
    13.     Public Function SumFunction()
    14.         MsgBox("<F1> and <Shift> Pressed")
    15.     End Function


    VB Code:
    1. ' Lunatic3's sample
    2.  
    3.         If e.KeyCode = Keys.F1 And e.Shift Then
    4.             MsgBox("<F1> and <Shift> were pressed")
    5.         End If
    6.         e.Handled = True
    Visual Baisc 6 (SP5)
    Windows Xp

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