PDA

View Full Version : Has anyone figured out how to get .Net TextBox to receive the Enter keystroke?


Jon-Huhn
01-13-2008, 05:47 PM
When searching the forums, I found that other people were having a similar problem. Interesting to note, however, that at least some people could get the KeyPress handler to print out the event.keyChar property of the Enter keystroke. When I try it, however, that handler isn't even fired.

Anyone have an luck with this yet?

Thanks!

Jon-Huhn
01-13-2008, 07:10 PM
Well, here's a little tidbit I discovered.

If I use the KeyUp event instead of the KeyPress or KeyDown, the Enter key does fire the event. However the event.keyChar property isn't available for the KeyUp event, so I still don't know how to detect what key was just released.

RustyKnight
01-13-2008, 11:35 PM
Well, here's a little tidbit I discovered.

If I use the KeyUp event instead of the KeyPress or KeyDown, the Enter key does fire the event. However the event.keyChar property isn't available for the KeyUp event, so I still don't know how to detect what key was just released.You have to use the KeyEventArgs that are passed in on the event to gain access to the "virtual" key...

Something like...on KeyUp keyEventArgs do (

local keyCode = keyEventArgs.keyCode
local vKeys = DotNetObject "System.Windows.Forms.Keys"

if keyCode = vKeys.Enter then (

-- Your code here to do with the enter key been pressed

)

)

Hope this helps
Shane

Jon-Huhn
01-14-2008, 12:34 AM
Shane,

I think you've got me on the right track, however something in your syntax is kicking an error for me. When I try to run:

DotNetObject "System.Windows.Forms.Keys"

I get the error, "No constructor found which matched argument list: System.Windows.Forms.Keys"

Is there something you left out? Thanks!

RustyKnight
01-14-2008, 12:39 AM
Shane,

I think you've got me on the right track, however something in your syntax is kicking an error for me. When I try to run:

DotNetObject "System.Windows.Forms.Keys"

I get the error, "No constructor found which matched argument list: System.Windows.Forms.Keys"

Is there something you left out? Thanks!No, I just get confused between these two, you want DoNetClass

Shane

Jon-Huhn
01-14-2008, 12:50 AM
Great, works as I had hoped. Thanks so much for your help.

Jon-Huhn
01-14-2008, 01:34 AM
I wonder if you could answer one more thing for me, Shane, if you have time.

Now that I have the ability to detect when the user hits the Enter key, I want to makes it so when the user hits the Enter key, it adds a new line to the textbox and jumps the cursor down to the start of that new line.

Right now I'm just doing it like this:

texbox.text+="\n"

This adds a new line, but it just moves the cursor to the beginning of the first line, and also adds square to the end of the first line.

Do you have any idea how to add a new line to a textbox and set the cursor there (and not have that ugly square character appear)? Do I set it with Maxscript or must I do it through DotNet somehow?

Thanks!

RustyKnight
01-14-2008, 02:17 AM
I wonder if you could answer one more thing for me, Shane, if you have time.

Now that I have the ability to detect when the user hits the Enter key, I want to makes it so when the user hits the Enter key, it adds a new line to the textbox and jumps the cursor down to the start of that new line.

Right now I'm just doing it like this:

texbox.text+="\n"

This adds a new line, but it just moves the cursor to the beginning of the first line, and also adds square to the end of the first line.

Do you have any idea how to add a new line to a textbox and set the cursor there (and not have that ugly square character appear)? Do I set it with Maxscript or must I do it through DotNet somehow?

Thanks!From what I've read, you're pretty close...

Two things, make sure you have set TextBox.MultiLine to true and do TextBox.Text = "First line\r\nSecond line"

But if that still makes you want to pull what little hair you have remaining (from experience) out of your head, you could try something like...local Environment = DotNetObject "System.Environment"
textBox.Text="First Line" + Environment.NewLine + "Second Line";and see where that takes you...

Jon-Huhn
01-14-2008, 02:31 AM
Hi, Shane

Both ways you just described worked well in creating a new line without that ugly square at the end of the line, but the cursor always pops back to very start of the textbox.

I'm gonna dig around Microsoft's site (shudder) and see if I can cobble together something that lets you set the exact position of the cursor within the TextBox.

You've been very helpful. Thanks!

Jon-Huhn
01-14-2008, 02:47 AM
Ok, things are looking pretty good now. I used texbox.SelectionStart=texbox.text.count to set the caret to the end of the file when return is hit. Now with a little more digging I can find out how to insert a line when Enter is hit instead of just adding a line to the end.

RustyKnight
01-14-2008, 02:52 AM
Ok, things are looking pretty good now. I used texbox.SelectionStart=texbox.text.count to set the caret to the end of the file when return is hit. Now with a little more digging I can find out how to insert a line when Enter is hit instead of just adding a line to the end.That's a really good solution, if you have time, also try looking at TextBox.appendText (i think) and see where that gets you...

Shane

Jon-Huhn
01-14-2008, 03:31 AM
Ok, here's the event handler that allows the user to hit return to insert a new blank line. Thanks Shane for helping me work through this, and I hope this is usefull to many others as well:



/*
Use this KeyUp event handler to detect when Enter has been hit
while the TextBox has the focus, and then insert a new blank line at that point.
*/

on TextBox KeyUp senderArg keyPressEventArgs do (

local keyCode = keyPressEventArgs.keyCode
local virtKeys = DotNetClass "System.Windows.Forms.Keys"
if keyCode == virtKeys.Enter then ( -- or if you're checking for the letter "a", use virtKeys.a etc...
local preString=substring TextBox.text 1 TextBox.SelectionStart -- everything in the string up to the cursor
local postString=substring TextBox.text (TextBox.SelectionStart+1) TextBox.text.count -- everything in the string after the cursor
TextBox.text=preString+"\r\n"+postString -- insert a new line in between the two sections
TextBox.SelectionStart=preString.count+2 -- force the cursor be at that new line
)
)

PEN
01-14-2008, 11:53 AM
Wow good work, I just gave up on all this when I hit this wall. Thanks for posting the solution.

CycloneS
08-20-2008, 06:00 AM
OK I got two events in the script below. keyDown and keyUP. The keyUp event works but the keyDown does not work. It works with the "letter" keys but not the arrowkeys, enter etc.


rollout window "key presses"
(

dotNetControl RH_Input "System.Windows.Forms.Control"

on window open do (RH_Input.focus())
on window mouseMove pos do (RH_Input.focus())

on RH_Input KeyDown senderArg keyPressEventArgs do
(
local keyCode = keyPressEventArgs.keyCode
local virtKeys = DotNetClass "System.Windows.Forms.Keys"
if keyCode == virtKeys.left then ( print "left")
)

on RH_Input KeyUp senderArg keyPressEventArgs do
(
local keyCode = keyPressEventArgs.keyCode
local virtKeys = DotNetClass "System.Windows.Forms.Keys"
if keyCode == virtKeys.right then ( print "right")
)
)
createDialog window 600 600


I came across these links that look useful.

KeyEventArgs Class
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.aspx

KeyPreview
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx

Control.KeyDown Event
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown.aspx

To handle keyboard events only at the form level and not enable other controls to receive keyboard events, set the KeyPressEventArgs..::.Handled (http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.handled.aspx) property in your form's KeyPress (http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx) event-handling method to true. Certain keys, such as the TAB, RETURN, ESCAPE, and arrow keys are handled by controls automatically. To have these keys raise the KeyDown event, you must override the IsInputKey (http://msdn.microsoft.com/en-us/library/system.windows.forms.control.isinputkey.aspx) method in each control on your form. The code for the override of the IsInputKey (http://msdn.microsoft.com/en-us/library/system.windows.forms.control.isinputkey.aspx) would need to determine if one of the special keys is pressed and return a value of true. "

So it seems I need to disable some keys using the isinputkey but i'm not sure how. KeyPreview also looks promising. I think i'm on the right track but don't know the specifics. Any ideas?


Thanks.

CGTalk Moderation
08-20-2008, 06:00 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.