Results 1 to 13 of 13

Thread: hotkeys (un-fixed)(re-fixed*self solved*)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Location
    /root/
    Posts
    94

    Talking hotkeys (un-fixed)(re-fixed*self solved*)

    this is my first post. weee!!

    i am trying to make hotkeys for an event of a program i am making.

    i have this so far but it wont work.
    VB Code:
    1. Public Class Form1
    2.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    3.         If System.Windows.Forms.Keys.F7 Then
    4.             My.Computer.Keyboard.SendKeys(TextBox1.Text)
    5.             My.Computer.Keyboard.SendKeys("{enter)")
    6.         End If
    7.  
    8.     End Sub
    9. End Class
    i understand why it wont work, but i cant figure out what to write to make it work.
    i think it is the "If System.Windows.Forms.Keys.F7 Then" part.
    could someone help me?
    Last edited by ader10; Jul 4th, 2005 at 03:15 PM.

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: hotkeys

    Put the code in the Form1_KeyDown event, after you set the KeyPreview property of the form to True And you need to change the If statement a bit but I'm sure you can get it

    VB Code:
    1. Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    2.  
    3.         If e.KeyCode = Keys.Right Then
    4.             MsgBox("a")
    5.         End If
    6.     End Sub


    Has someone helped you? Then you can Rate their helpful post.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Location
    /root/
    Posts
    94

    Re: hotkeys

    fank yoo ber mush.
    i have never heard of "e". probably because i am in mid-migration to .net
    but how do i get it to type the text in textbox1 over again?
    Last edited by ader10; Jun 30th, 2005 at 03:38 PM.

  4. #4
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: hotkeys

    Sorry, but I don't understand what you mean by "how do i get it to type the text in textbox1 over again"... Could you explain that again?

    Thanks


    Has someone helped you? Then you can Rate their helpful post.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: hotkeys

    Quote Originally Posted by ader10
    fank yoo ber mush.
    i have never heard of "e". probably because i am in mid-migration to .net
    but how do i get it to type the text in textbox1 over again?
    Notice that your Timer.Tick event handler has an "e" argument as well. Every event handler has a very similar signature: sender As Object, e As EventArgs, but the type of e can be different, depending on the type of event. Whatever its type, it holds information specific to the event, while the sender is the object that raised the event.

    Now to your issue. Obviously you want something to happen when you press F7. What exactly do you want to happen and under what circumstances? Are you aware that you can assign shortcut keys to menu items? This may not be applicable in your case, but if the action you want to perform corresponds to a menu option then it is the simplest way to achieve what you want.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Location
    /root/
    Posts
    94

    Re: hotkeys

    this should answer both of your questions.


    Quote Originally Posted by manavo11
    Sorry, but I don't understand what you mean by "how do i get it to type the text in textbox1 over again"... Could you explain that again?

    Thanks

    i have textbox1 and timer1.
    i want the text in textbox1 to be typed automatically by the computer and then have the computer press enter by using sendkeys when i press f7

  7. #7
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: hotkeys

    first set your form property keypreview to true.
    just like manavo11's code
    VB Code:
    1. Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    2.  
    3.         If e.KeyCode = Keys.F7 Then
    4.             MsgBox("a")
    5.         End If
    6.     End Sub

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: hotkeys

    First of all, are you using VB 2005? I notice the My.Computer.Keyboard.Sendkeys and think that looks like .NET 2.0. If that is the case, it is always best to mention it from the outset. VS.NET 2003 and .NET 1.1 are still the standards and most people will assume you are using them unless you say otherwise.

    As for your question, I don't really see why you are trying to equate the F7 key with the ENTER key. Why doesn't the user just hit the ENTER key? Otherwise, set the TextBox's Text in the Timer's Tick event handler and simulate your key press in the Form's KeyDown event handler, as suggested by others.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Location
    /root/
    Posts
    94

    Re: hotkeys

    Quote Originally Posted by jmcilhinney
    First of all, are you using VB 2005? I notice the My.Computer.Keyboard.Sendkeys and think that looks like .NET 2.0. If that is the case, it is always best to mention it from the outset. VS.NET 2003 and .NET 1.1 are still the standards and most people will assume you are using them unless you say otherwise.

    As for your question, I don't really see why you are trying to equate the F7 key with the ENTER key. Why doesn't the user just hit the ENTER key? Otherwise, set the TextBox's Text in the Timer's Tick event handler and simulate your key press in the Form's KeyDown event handler, as suggested by others.
    ehmmm... thats not what i am trying to do. (yes i am using vb2005. but i use both.)
    i dont get the second part of your post probly because you dont get my post.
    let me explain it with more detail.
    •••••••••••••••••_____________
    i have textbox1. [this is textbox1]
    i have timer1. this is timer1: (+)
    this is my keyboard key, f7. [F7]
    •••••••••••••••••
    when i press [F7], i want the text in textbox1 to be "typed by the computer" and then have the computer type the [enter] key immediately after typing the text. i want this to keep happening forever at a timed interval (eg, 3 seconds apart) until i press [F7] again.
    [F7] is my toggle key to start/stop typing then pressing enter.
    so if i have "asdf" in the textbox, and i have notepad open, and if i run the program and set the window focus on notepad, the text should appear in notepad's text box.

    asdf
    asdf
    asdf
    asdf
    asdf
    asdf

    ***•••*** then i want to stop it. so i go to the taskbar, press the program's button to get focus on the program, and press [F7] to stop the text from typing.
    remember, [F7] is my toggle key to start/stop typing then pressing enter.

    "sendkeys Sends one or more keystrokes to the active window, as if typed on the keyboard."

    if you still dont understand, just yelp.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: hotkeys

    Ahhhh... now I get it. You pretty much had it to start with. Remove the If statement form your Tick event handler and just enable and disable the Timer in the form's KeyUp (or KeyDown) event handler:
    VB Code:
    1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    2.         SendKeys.Send(Me.TextBox1.Text)
    3.         SendKeys.Send("{ENTER}")
    4.     End Sub
    5.  
    6.     Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
    7.         If e.KeyCode = Keys.F7 Then
    8.             'Toggle the timer's state.
    9.             Me.Timer1.Enabled = Not Me.Timer1.Enabled
    10.         End If
    11.     End Sub
    Make sure that the form's KeyPreview property is set to True. You may also want to provide some visual cue to indicate whether the Timer is enabled or not.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Location
    /root/
    Posts
    94

    Re: hotkeys

    congratulations!
    you win the prize! you win a gmail account, if you dont have one.
    pm me.

    the complete code at the end was leeched, but who cares? it works now.
    this is my complete code:
    VB Code:
    1. Public Class Form1
    2.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    3.         'Type what is in textbox1.
    4.         SendKeys.Send(Me.TextBox1.Text)
    5.         'Press enter immediately after
    6.         SendKeys.Send("{ENTER}")
    7.     End Sub
    8.  
    9.     Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
    10.         If e.KeyCode = Keys.F7 Then
    11.             'Toggle the timer's state.
    12.             Me.Timer1.Enabled = Not Me.Timer1.Enabled
    13.             'If F7 is pressed, timer1.enabled is switched.
    14.         End If
    15.     End Sub
    16. End Class

    now i need to tackle transparency and the "boss key"
    but i wont have problems with that, i think...

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: hotkeys (fixed)

    Just keep in mind that SendKeys.Send causes behaviour exactly as though you used the keyboard. If some other application gets the focus while your Timer is enabled, the key presses will get sent to it instead of the one you want.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Location
    /root/
    Posts
    94

    Re: hotkeys (un-fixed)

    it doesnt work in visual basic .net 2003 standard.

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