6 Replies - 4415 Views - Last Post: 26 December 2012 - 10:04 AM Rate Topic: -----

#1 killergod647  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-December 12

Keypress Problem

Posted 25 December 2012 - 10:27 AM

Hey guys,

Dim x as String = "Space"
Dim y as String = "LControlKey"
If GetAsyncKeyState(x) = -32768 And GetAsyncKeyState(y) = -32768 Then
                AccessControl()
            End If


What I'm trying to do with this piece of code is, check if the button contained within the string x/y is being pressed. So, for example, if x = "H" and y = "LControlKey" I wanna check if "H" and the "LControlKey" are being pressed at the sametime. The problem I'm facing is that x/y are strings and I'm not sure how to convert them into keypresses so that I can use GetAsyncKeyState to check if they're pressed or not.

Sorry if it's a little confusing, but anyone know hwo to fix this?

Another example,
Lets say x = "Shift" and y = "Enter", then I wanna check if Shift and Enter are being pressed at the sametime. Thanks again.

Is This A Good Question/Topic? 0
  • +

Replies To: Keypress Problem

#2 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Keypress Problem

Posted 25 December 2012 - 10:32 AM

Why don't you use the keypress events for said keys?

Brb will elaborate more on how to do this, unless of course anyone else chips in first.

If you use the KeyDown event of the form and set key Preview to true then it will pick up the key presses. You can set an if statement in there to check for modifiers. Enter, Ctrl, Alt, and Shift are all modifiers to key presses.

    Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.G AndAlso e.Modifiers = Keys.Shift Then
            MessageBox.Show("Both keys pressed!")
        End If
    End Sub



You can also use a boolean to check for the keys.

    Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.Control = true or e.Alt = true or e.Shift = true AndAlso e.Modifiers = Keys.Shift Then
            MessageBox.Show("Blam!")
        End If
    End Sub


This post has been edited by trevster344: 25 December 2012 - 11:06 AM

Was This Post Helpful? 0
  • +
  • -

#3 killergod647  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-December 12

Re: Keypress Problem

Posted 25 December 2012 - 10:23 PM

View Posttrevster344, on 25 December 2012 - 10:32 AM, said:

Why don't you use the keypress events for said keys?

Brb will elaborate more on how to do this, unless of course anyone else chips in first.

If you use the KeyDown event of the form and set key Preview to true then it will pick up the key presses. You can set an if statement in there to check for modifiers. Enter, Ctrl, Alt, and Shift are all modifiers to key presses.

    Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.G AndAlso e.Modifiers = Keys.Shift Then
            MessageBox.Show("Both keys pressed!")
        End If
    End Sub



You can also use a boolean to check for the keys.

    Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.Control = true or e.Alt = true or e.Shift = true AndAlso e.Modifiers = Keys.Shift Then
            MessageBox.Show("Blam!")
        End If
    End Sub



Right, but the thing is I want them to be able to change the hotkey. So essentially, this is used as a hotkey to make the program appear, and to hide it. (You press the hotkey if the form is shown it becomes hidden, and if the form is hidden it becomes shown). To allow them to specify what they wanted the hotkey to be I was using a keyboard hook to capture what they pressed after they hit "set hotkey", so if they hit "set hotkey" and pressed "Alt and Control", I would get the input "<ControlKey> + <MenuKey>" as a string. How can I allow them to choose the hotkey? The other thing is, since the form might be hidden I need to make sure that it can capture the keys when the form is not in focus. Currently I used a thread to do this. Any advice on how to fix my issue, or how to change what I'm doing so it works?

Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Keypress Problem

Posted 25 December 2012 - 10:48 PM

Show me your code and I'm sure I will be able to help you out sir.
Was This Post Helpful? 0
  • +
  • -

#5 borkusgod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-December 12

Re: Keypress Problem

Posted 26 December 2012 - 03:41 AM

Another app that you may be interested in because it was primarily made with your process in mind is AutoHotKey . It's a handy little app and is a lot more powerful then it seems at first. Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#6 C.Andrews  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 169
  • Joined: 18-October 12

Re: Keypress Problem

Posted 26 December 2012 - 09:08 AM

View Postborkusgod, on 26 December 2012 - 03:41 AM, said:

Another app that you may be interested in because it was primarily made with your process in mind is AutoHotKey . It's a handy little app and is a lot more powerful then it seems at first. Hope that helps.


Second this. AutoHotKey is amazing, I keep finding new uses for it all the time. Also a great app to pick apart for learning purposes.
Was This Post Helpful? 0
  • +
  • -

#7 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Keypress Problem

Posted 26 December 2012 - 10:04 AM

If we continue this in private chat sir I can help. I have already written code to do all of this, but I won't share it here as it can be used maliciously by any would be programmers who thinks they need to cheat people with a keylogger.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1