Why is a C# console processing multiline paste as Enter key presses? - c#

I have a C# application which handles key presses and will break the loop when the Enter key is pressed. The problem I am encountering is that when a multiline paste event occurs, the console is interpreting the newlines as Enter key presses.
Example that doesn't cause Enter keypress:
select something \n from somewhere \n where condition
Example that does cause Enter keypress:
select something
from somewhere
where condition
Does anyone know why this would be? And is there any way to stop this without digging into the WndProc stuff for intercepting a Paste event?

On Windows, new lines are \r\n. When you press the Enter key, these two characters are input. So if you put a \r before each of your \n they should be treated as newlines.

Hi it looks like someone used Console.ReadLine() to trigger loop break.
https://msdn.microsoft.com/pl-pl/library/system.console.readline(v=vs.110).aspx
Effectively loop breaks when you start new line not only press enter Key.

The Enter key press can't be resolved back into \r\n as far as I can tell, so I don't think we can process based on those characters - but I was able to get a hint (for my situation anyway) if the user was pasting multiline text by interrogating Console.KeyAvailable.
So now, I look to see if (Key == ConsoleKey.Enter && Console.KeyAvailable == false) to determine whether to process it as "genuine" Enter key press or not (as there usually aren't keys available in my situation when Enter is pressed).
See this commit for more info

Related

Partially read-only textbox in c#

I've got a list of illegal positions and characters that can't move from those positions. How would I prevent those from being modified in the TextChanged event? Every solution I've come up with has been extremely hacky and unreliable, or has relied on the KeyDown event, which doesn't prevent the user from deleting the read-only text in other ways (select the text and delete it, or just press the backspace key).
I've thought of doing something like this:
//CharPos is a class with an int (CharPos.Position) and a char that
//should be at that int's position (CharPos.Ch)
foreach (CharPos p in IllegalPositions)
{
console.Text = console.Text.Remove(p.Position, 1);
console.Text = console.Text.Insert(p.Position, p.Ch.ToString());
}
But it completely messes up and goes on an infinite loop. And even if I stopped it from doing that, it wouldn't work if you changed the character count of the text, by using the delete key for example. Maybe I could use regex somehow? (I dunno, I have no experience at all with regex).
Can you combine multiple TextBoxes (for editable parts) with TextBlocks (for readonly parts)? You would just need to play with styling of the two to match
Could you please check the link below:
Disabling or making it readonly a part of the values in a Text Box (.net)
rtBox.Select(0, (rtBox.Text = "I am fixed content").Length);
rtBox.SelectionProtected = true;
rtBox.AppendText(Environment.NewLine);

c# removing whole text when it is displayed

I would like to ask you for help. How i am supposed to remove whole text when i press a button, (for instance 1). Is there a way to do that?
The problem is on c# console application
With Console.Clear() you can remove every Text from the console.
You can use
Console.Clear();
to clear the console window. This removes the whole text from the console window.
Or send a delete character to move the cursor backwards:
Console.Write("\b");
The you can overwrite it with another character.
char c = Console. ReadKey().KeyChar ;
Capture the key and overwrite it with blank

c# backspace simulator - deletes entire word instead of just a letter?

I am writing a simple C# program in which when I press "ctrl+g" I want my program to automatically delete the character directly left of the cursor in any program (ex: chrome browser, word document, powerpoint document, etc...).
I have installed a global hook for "ctrl+g" and it works fine. I am using a keyboard simulator that I found from codeproject: http://www.codeproject.com/Articles/28064/Global-Mouse-and-Keyboard-Library
My issue is that when I simulate a backspace like so:
KeyboardSimulator.KeyPress(Keys.Back);
the entire word is deleted instead of just the character to the left of the cursor. For example, if I am in a Microsoft Word document with the following line of text:
"Happy new year"
if my cursor is at the end of "year" and I press ctrl+g, my program deletes "year" and puts the cursor just to the right of "new" instead of just deleting the letter "r" of "year".
I have tried other simulators as well with the same result. Does anyone have a solution or know what I am doing incorrectly?
Thanks.
Apparently, Ctrl+Backspace commonly means delete the entire word in Windows. When you press Ctrl+g, and your hook simulates the back space key, the program sees Ctrl+Backspace because the Ctrl is down.

How to use 0x08

How do you use the ASCII value of backspace 0x08 programmatically in a WinForms TextArea? I tried in a console program:
Console.WriteLine("Hello" + (char)0x8 + "World");
And it shows as I expected:
HellWorld
Where as I tried in a WinForms TextArea to fill the value:
txt.Text += "Hello" + (char)0x8 + "World";
It shows like this:
What to make backspace event to trigger other than using Keyboard's event to make the display as I expect by using the value of 0x8 alone not accessing with any text field events. Through programmatically to print "HellWorld" in text field.
Characters and keypress are not related directly. You would have to get a Backspace keypress into the textbox in order to erase a character.
If you want to simulate someone typing in the textbox, then you might do so using SendKeys.Send(), e.g:
txt.Select();
SendKeys.Send("hello");
SendKeys.Send(((char)0x8).ToString()); // send backspace using 0x08
SendKeys.Send("{BS}"); // send backspace using predefined code
SendKeys.Send("world");
But pre-processing the strings seems much simpler (and safer), e.g:
var str = "hello";
txt.Text += str.Substring(0, str.Length-1);
Note: SendKeys.Send sends keystrokes to the active window. So if another window gets the focus while the above code is running, then the keystrokes might end in a different window.
The backspace character was introduced for thin clients that send input to a server character by character, to allow end users to fix a typo that was already sent. Modern applications usually have no use for such a character, as client applications now usually just wait until the user has finished entering data before sending it to the server.
That is why the TextBox control does not have support for it, and probably why people are wondering why you need this.
Anyway, you'll need to pre-process the data yourself before setting the TextBox's Text. You could for instance use regular expressions to simulate this behavior.

C# Programmatically send two keys when you are already pressing another one

I'm making my own multiple clipboard copy/paste tool which runs in background, and I've finally achieved it, it's working.
this is how it works,
when Capslock it's pressed, if I press CTRL+1 I make a CTRL+C programmatically ( with SendKeys) and save the clipboard on my list with correct position
when capslock it's not pressed, if I press CTRL+1 I make a CTRL+V programmatically ( with send keys) by using the latest data in clipboard on the correct list position.
Now it's fine, but I want to make a little change, I don't want to use capslock, but I want to press another key, like ALT or SHIFT, but if you keep pressing a key which is not the CTRL and then you do a CTRL+C, it does not work.
Anyone have any advice to this dumb thing?
Thanks guy
If you don't mind using WinApi functions, you can use RegisterHotKey and UnregisterHotKey functions.
They allow you to register and unregister global shortcuts of your choice. This way you'll get notified about a shortcut being pressed even if your application is running in background and doesn't have the focus on itself.
You can find more information about both functions on pinvoke here and here. There is even some sample application code so you can see how to use them.
NOTE: Remember to unregister all the shortcuts that you've registered on application exit.
You can use a flag for the select key. When you check to see what keys are pressed then:
something like the following pseudo code should do the trick:
if(select key)
{
this.selectKey = true;
}
else
{
if(ctrl key)
{
//do whatever you would normally do here
}
this.selectKey = false;
}

Categories