Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Generic User Avatar

Assign Function To Key Press?


  • Please log in to reply
26 replies to this topic

#1 Wolfy87

Wolfy87

  •  Avatar image
  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:08:36 AM

Posted 25 September 2008 - 04:24 PM

Is it posible to assign a function (eg private void fKey_press etc) to the press of a keybord key?
Thanks,
Wolfy87.

BC AdBot (Login to Remove)

 


#2 Billy O'Neal

Billy O'Neal

    vcpkg Maintainer


  •  Avatar image
  • Malware Response Team
  • 12,305 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:11:36 PM

Posted 25 September 2008 - 04:32 PM

http://msdn.microsoft.com/en-us/library/sy...l.keypress.aspx :thumbsup:


First google result....

Billy3

#3 Wolfy87

Wolfy87
  • Topic Starter

  •  Avatar image
  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:08:36 AM

Posted 25 September 2008 - 04:51 PM

I cant say that i know what in the world the C# example is going on about :|

#4 Billy O'Neal

Billy O'Neal

    vcpkg Maintainer


  •  Avatar image
  • Malware Response Team
  • 12,305 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:11:36 PM

Posted 25 September 2008 - 04:52 PM

Do you know how to bind a function to an event?

Billy3

#5 Wolfy87

Wolfy87
  • Topic Starter

  •  Avatar image
  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:08:36 AM

Posted 25 September 2008 - 04:55 PM

No, i do not.

#6 Billy O'Neal

Billy O'Neal

    vcpkg Maintainer


  •  Avatar image
  • Malware Response Team
  • 12,305 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:11:36 PM

Posted 25 September 2008 - 04:59 PM

Here's a hint.. look at the forms designer generated code. Pressing a button triggers an event. As does each character typed into a textbox. As well as everything else....

Billy3

#7 Wolfy87

Wolfy87
  • Topic Starter

  •  Avatar image
  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:08:36 AM

Posted 25 September 2008 - 05:04 PM

Im on my psp and its 11 at night here so i cant just yet :thumbsup:

100'th Post =D

#8 Wolfy87

Wolfy87
  • Topic Starter

  •  Avatar image
  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:08:36 AM

Posted 26 September 2008 - 10:03 AM

// 
// button1
// 
this.button1.Location = new System.Drawing.Point(6, 15);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(43, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Close";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);

/\ Here is an extract from my tester form /\

Would the keyDown process be used, and would it be in the bottom line?
this.FKey.KeyDown += ....... ???

#9 Billy O'Neal

Billy O'Neal

    vcpkg Maintainer


  •  Avatar image
  • Malware Response Team
  • 12,305 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:11:36 PM

Posted 26 September 2008 - 02:49 PM

I'm honestly not 100% sure, I'm not firmiliar with CSharp specificly. But I do know that the forms designer has to register those events to make things work.
Googling CSharp events will probably give you the answer...

Also, events registered in this way are not global. They would only have effect while your form has focus. For global keys I think you need to break out into unmanaged code land. Not fun.

Billy3

#10 Wolfy87

Wolfy87
  • Topic Starter

  •  Avatar image
  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:08:36 AM

Posted 26 September 2008 - 03:56 PM

Isnt there just a simple way of creating a function that activates when for example F.KeyDown?

#11 PlyPencil

PlyPencil

  •  Avatar image
  • Members
  • 51 posts
  • OFFLINE
  •  
  • Local time:08:36 AM

Posted 27 September 2008 - 02:56 AM

C# and VB are much the same. Goto the form events and choose the KeyPressed one then the code will be something like this

if e.keys.k = true then
-->commands here
end if

so every time you press a key it will run the if statement, but it will not do the commands unless it is the key you specified in this case K

#12 Wolfy87

Wolfy87
  • Topic Starter

  •  Avatar image
  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:08:36 AM

Posted 27 September 2008 - 05:27 AM

A brilliant consept and exactly what i was looking for and i think i have a bit of the code but im definatly missing somthing...

public void KeyPressEventArgs(object sender, EventArgs e)
{
if (Keys.A)
textBox9.Text = "It worked!"; 
}

The problem is with Keys.A, they are legal characters, they get bung up in the help box when the word has been started ie when you type "Ke" it automatically fills it in with "Keys" for me so all i i need to do is find the bit of code i am missing. a great suggestion all the same Ply, thanks

#13 Wolfy87

Wolfy87
  • Topic Starter

  •  Avatar image
  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:08:36 AM

Posted 27 September 2008 - 06:08 AM

Ive refined it a bit by researching on MSDN but it still dose not return anything when the key is pressed?...

private void keypressed(Object o, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.A)
textBox9.Text = "It Worked!";
}

It runs and debugs but when the A key is pressed... nothing?

#14 PlyPencil

PlyPencil

  •  Avatar image
  • Members
  • 51 posts
  • OFFLINE
  •  
  • Local time:08:36 AM

Posted 27 September 2008 - 08:19 AM

Try creating an application which will write text in a textbox when you press a key, and the textbox displays the letter. This will tell you where in the code the problem is, it could be your method or argument.

textbox9.text = e.KeyChar.tostring

Edited by PlyPencil, 27 September 2008 - 08:23 AM.


#15 Billy O'Neal

Billy O'Neal

    vcpkg Maintainer


  •  Avatar image
  • Malware Response Team
  • 12,305 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:11:36 PM

Posted 27 September 2008 - 11:35 AM

It should be noted that the windows forms designer has native support for AutoComplete.....

Take a look at the properties pane :thumbsup:

Billy3




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users