Find out if any key is pressed

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 28959 Views - Last Post: 24 February 2010 - 06:06 AM Rate Topic: -----

#1 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Find out if any key is pressed

Posted 14 February 2010 - 03:41 PM

Hi
How do I find out if any key is pressed during a certain time?
i have a timer to the "certain time" is no problem.
But how do I find if any key is pressed?
i think i would need "e.keycode" but iīm not sure and i cant find anything on google.


Thanks in advance
Is This A Good Question/Topic? 0
  • +

Replies To: Find out if any key is pressed

#2 xnn  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 227
  • Joined: 10-February 10

Re: Find out if any key is pressed

Posted 14 February 2010 - 03:57 PM

I'm not real sure what you are trying to do but to capture a key press use the KeyPress event on the control you'd like to capture.

    Private Sub Control_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
' e.keychar returns the key that was pressed.

Messagebox.show("The key pressed was: " & e.keychar)
 
    End Sub



I'm not real sure what you are trying to do but to capture a key press use the KeyPress event on the control you'd like to capture.

    Private Sub Control_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
' e.keychar returns the key that was pressed.

Messagebox.show("The key pressed was: " & e.keychar)
 
    End Sub


Was This Post Helpful? 0
  • +
  • -

#3 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Find out if any key is pressed

Posted 14 February 2010 - 04:01 PM

GetKeyState() method of user32.dll might be what you need:
http://msdn.microsof...301(VS.85).aspx
Was This Post Helpful? 0
  • +
  • -

#4 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Find out if any key is pressed

Posted 14 February 2010 - 04:16 PM

View Postjanne_panne, on 14 February 2010 - 03:01 PM, said:

GetKeyState() method of user32.dll might be what you need:
http://msdn.microsof...301(VS.85).aspx

maybe.. iīve lookīt in to it but i dont understand how to use it.

View Postxnn, on 14 February 2010 - 02:57 PM, said:

I'm not real sure what you are trying to do but to capture a key press use the KeyPress event on the control you'd like to capture.

    Private Sub Control_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
' e.keychar returns the key that was pressed.

Messagebox.show("The key pressed was: " & e.keychar)
 
    End Sub


iīm trying to just like windows:
when you have not touchīt your computer for a certain time the it should do something.
i already have the code to check if the mouse has been moved.
Now i only need to check if any key has been pressed.
Was This Post Helpful? 0
  • +
  • -

#5 xnn  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 227
  • Joined: 10-February 10

Re: Find out if any key is pressed

Posted 14 February 2010 - 05:58 PM

Oh, I thought you were talking about a windows form. I believe you need to use window hooks from user32.dll. The following is a tutorial of keyboard hooks: http://www.developer...s-in-VB-NET.htm
Was This Post Helpful? 0
  • +
  • -

#6 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Find out if any key is pressed

Posted 14 February 2010 - 06:03 PM

View PostEndLessMind, on 14 February 2010 - 03:16 PM, said:

View Postjanne_panne, on 14 February 2010 - 03:01 PM, said:

GetKeyState() method of user32.dll might be what you need:
http://msdn.microsof...301(VS.85).aspx

maybe.. i´ve look´t in to it but i dont understand how to use it.

View Postxnn, on 14 February 2010 - 02:57 PM, said:

I'm not real sure what you are trying to do but to capture a key press use the KeyPress event on the control you'd like to capture.

    Private Sub Control_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
' e.keychar returns the key that was pressed.

Messagebox.show("The key pressed was: " & e.keychar)
 
    End Sub


i´m trying to just like windows:
when you have not touch´t your computer for a certain time the it should do something.
i already have the code to check if the mouse has been moved.
Now i only need to check if any key has been pressed.

Declare a flag variable, say a boolean, globally to both the KeyPress() event and your Timer_Tick() code. When the timer starts clear the flag. In the KeyPress() event set the flag. When the timer fires, check the flag state. If it's set the keyboard was active. If not, not.

This post has been edited by n8wxs: 14 February 2010 - 06:04 PM

Was This Post Helpful? 0
  • +
  • -

#7 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Find out if any key is pressed

Posted 14 February 2010 - 11:45 PM

View Postn8wxs, on 14 February 2010 - 05:03 PM, said:

Declare a flag variable, say a boolean, globally to both the KeyPress() event and your Timer_Tick() code. When the timer starts clear the flag. In the KeyPress() event set the flag. When the timer fires, check the flag state. If it's set the keyboard was active. If not, not.

Sorry. I dont really know what you mean.
Was This Post Helpful? 0
  • +
  • -

#8 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Find out if any key is pressed

Posted 14 February 2010 - 11:54 PM

View Postxnn, on 14 February 2010 - 04:58 PM, said:

Oh, I thought you were talking about a windows form. I believe you need to use window hooks from user32.dll. The following is a tutorial of keyboard hooks: http://www.developer...s-in-VB-NET.htm

that did´nt make any sense =/
then i look´t at it.. it all look´t like "Mumbo Jumbo" to me

This post has been edited by EndLessMind: 14 February 2010 - 11:56 PM

Was This Post Helpful? 0
  • +
  • -

#9 devilydevil  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 14
  • Joined: 11-February 10

Re: Find out if any key is pressed

Posted 15 February 2010 - 12:04 AM

I have the exact code for this, but I will not give it out as it could be used for malicious purposes. Sorry and good luck, *hint...uses the GetKeyState() as others stated*
Was This Post Helpful? 0
  • +
  • -

#10 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Find out if any key is pressed

Posted 15 February 2010 - 12:07 AM

View Postdevilydevil, on 14 February 2010 - 11:04 PM, said:

I have the exact code for this, but I will not give it out as it could be used for malicious purposes. Sorry and good luck, *hint...uses the GetKeyState() as others stated*

I don't expect anyone to give out codes. And i donīt know how to use the GetKeyState.
The links that where posted here before was confusing for me.
Was This Post Helpful? 0
  • +
  • -

#11 begbodyguard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 15-February 10

Re: Find out if any key is pressed

Posted 15 February 2010 - 02:58 AM

I think this is what you need:
  Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
'Will stop timer if pressed any key'
        Timer1.enabled = false       
    End Sub

If you want a specific key
  Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        
        Select Case e.KeyValue
            Case Is = Windows.Forms.Keys.A
                timer1.enabled = false
            Case Is = Windows.Forms.Keys.Space
                 timer1.enabled = false

        End Select
    End Sub

This post has been edited by begbodyguard: 15 February 2010 - 02:58 AM

Was This Post Helpful? 0
  • +
  • -

#12 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Find out if any key is pressed

Posted 15 February 2010 - 05:13 AM

View Postbegbodyguard, on 15 February 2010 - 01:58 AM, said:

I think this is what you need:
  Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
'Will stop timer if pressed any key'
        Timer1.enabled = false       
    End Sub

If you want a specific key
  Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        
        Select Case e.KeyValue
            Case Is = Windows.Forms.Keys.A
                timer1.enabled = false
            Case Is = Windows.Forms.Keys.Space
                 timer1.enabled = false

        End Select
    End Sub

yes.. i get your point. but that would that to long time and to much code for doing more or less nothing.
because i have to add a new case for each button there is.

Itīnt there a way to do like
For Each "keypoess" in form.controls
"do this"
Next
?
Was This Post Helpful? 0
  • +
  • -

#13 begbodyguard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 15-February 10

Re: Find out if any key is pressed

Posted 15 February 2010 - 05:48 AM

Evry time you press at a key he will do your code.
  Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyPress
     "do this"
    End Sub

Was This Post Helpful? 0
  • +
  • -

#14 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Find out if any key is pressed

Posted 15 February 2010 - 05:51 AM

View Postbegbodyguard, on 15 February 2010 - 04:48 AM, said:

Evry time you press at a key he will do your code.
  Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyPress
     "do this"
    End Sub

Well. Iīve tried
  Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyPress
     Dim i as integer
i += 1
label1.text = i
    End Sub


but the labelīs text do not change.
Was This Post Helpful? 0
  • +
  • -

#15 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Find out if any key is pressed

Posted 15 February 2010 - 06:17 AM

Cause it won't change. i is always going to be 1.

This post has been edited by AdamSpeight2008: 15 February 2010 - 06:39 AM
Reason for edit:: damn new keyboard.

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3