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

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;
}

Related

How to detect Ctrl+Shift+Numeric Keypad in WinForms application

I would like to be able to allow users of my application to press Ctrl+Shift+numeric keypad keys to act as a navigational shortcut in an application I am developing. To my surprise it appears to be impossible to recognise this key combination.
Pressing Shift+keypad activates the extended functions displayed on most keyboards, such as cursor keys (on 2, 4, 6 and 8), page down/up (on 3 and 9), end/home (on 1 and 7), etc.
The various key events in WinForms report these exactly as if they were the actual cursor keys etc. being pressed. I'm therefore unable to distinguish between e.g. Shift+KeyPad4 and CursorLeft. I can't simply look for Ctrl+CursorLeft instead of Ctrl+Shift+KeyPad4, as people may be using Ctrl+CursorLeft for text editing purposes.
Is there any way I can properly detect these shifted numeric keypad keyboard combinations?
Thanks!
Use a class to keep track of what keys are pressed.
When you receive an OnKeyDown event, store that key as down and then when you come to When you receive an OnKeyUp event, remove that key from the store.
Then on the same OnKeyDown event, after storing the key press, do a check to see if all three keys that you expect are down.
It would look something like so:
public void OnKeyDown(EventArgs ...) {
_keyStore.KeyDown(eventArgs.keyPress);
if(_keyStore.AltCtrlKeyPad4IsDown()) { //Abstract this so you can perform multiple checks.
//Do shortcut.
}
}
public void OnKeyUp(EventArgs ...) {
_keyStore.KeyUp(eventArgs.keyPress);
}
Note that this is sudo code and will not compile.

perform a hotkey when press a graphical button

I got a thought experiment with a graphical user interface for writing a command.
There are buttons for saving, writing cursive and so on.....
Now when i press the button save, i want to that the compiler perform Ctrl+S like in every textdocument, or change the marked one to cursive with Ctrl+Shift+K.
I tried to set the variable to the value of the ascii code, but this ainĀ“t worked. Now i need help that the compiler perform the hotkey.
Is there a function in C# or is there another way to realize that.
Actually those hotkeys are implemented functions of the programs you use (e.g. word). The hotkey just executes the same function as the button you can press - the compiler doesn't know the hotkey Ctrl+Shift+K.
Just think about it: If the compiler knew about those hotkeys, it would have to know how to make the selected text in the focused textbox cursive - wouldn't this be kind of strange?
I might misunderstand your question, maybe you just wanted to ask how to type text with a simulated keyboard for the form, because you already implemented the shortcut functionability. In that case you'd have two options:
Call the function called when you press the specified shortcut
Actually simulate keypresses to the form
The second one is done like that:
SendKeys.Send("^S"); //For Ctrl+S - ^ is the Control button
For other special keys with the SendKeys function take a look at this: https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx.

count any keystrokes whether or not Window (App) is in focus - WPF

I'm building a "WPF Application" which is made to be run in the background (minimised state) and detects KeyStrokes of each and Every key on the keyboard & every Mouse Clicks.
So, my question is how to detect every keyStrokes whether app (Window) is minimised or not.
Simply, if my app is in focus then i use this code to count keystrokes.
Public int count;
protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e)
{
//base.OnKeyDown(e);
count++;
tBlockCount.Text = count.ToString();
}
I just want to do the same even if my app is minimised.
I've searched a lot and come across many suggestions like..
http://www.pinvoke.net/default.aspx/user32/registerhotkey.html
http://social.msdn.microsoft.com/Forums/vstudio/en-US/87d66b1c-330c-42fe-8a40-81f82012575c/background-hotkeys-wpf?forum=wpf
Detecting input keystroke during WPF processing
Detect if any key is pressed in C# (not A, B, but any)
Most of those are indicating towards Registering HotKeys. But I'm unable to match scenario with mine.
Any kind of suggestion are most welcome.
Although I'm not really condoning the use of a keylogger (This is what you are trying to do). I would recommend taking a look at this q/a, the section near the bottom of this article, and this article for some inspiration. These should help point in the right direction for the coding side.
What you essentially need to do is just set up an event to intercept any keys that come in from the computer, then you can gather the key and do whatever you like with it (in your case, record it)
Edit: In fact, reading the third article, it actually gives a full code snippet on how to implement and use it in WPF, so I recommend just reading that one.

How to make another Key acting like Enter

my question is kinda simple but I didn't find any answer.
I have a WPF application and I want to remap the behaviour of Tab and Enter keys onto other keys, because I need them to be close on the keyboard.
I already managed to make a letter key (for example J) acting like Tab Key, but I can't find a way to make a close button (for example K or H) acting like Enter!
For instance, I need this because the application works only with keyboard, so when an element (for example a button) has focus, I would like to press K or H to act like Enter key and do something. All works fine if I press the real Enter key but as I said, I would like to remap this function to another Key.
Is this possible? If so, how to do it in a WPF application?

how to identity a click out of the form and 1 more question

i want to build a simple program which help you selecting pictures.
if you have lot of picture and you want to choose some of them then you see them 1 by 1 and when you see a picture you would lik to save on other folder on your pcyou just press a button ,lets say f5 and the program copy the phtot from the path you looking at to the destiny folder.
for that program i need to ask how to know if someone pressed f5 out of the form area and how to know in which path the user looking at.(i want to build it for myself atm so if its help i look with microsoft office picture manager)
about the clicking i search a little and get something named global clicking and hooks which i dont understand so much and about identify the path i have no idea .
tyvm for help:)
I'm not sure I follow the rest -- but if you want to capture the keypress event, simply add an event handler for KeyPress and determine if the pressed key is equal to the F5 button by using the Keys constants.
Here is a project on Code Project that does exactly what you need :)
http://www.codeproject.com/KB/cs/globalhook.aspx
The key press event wont work with the following (Link):
TAB
INSERT
DELETE
HOME
END
PAGE UP
PAGE DOWN
F1-F2
ALT
Arrow keys
Note: I think there is a typo on the page and the F1-F2 really should be F1-F12.
When you decide the key for your on key press event for the form area you are talking about it will look like this:
private void Form_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == [keypressvalue])
{
//do your copy logic
}
}
[keypressvalue] will be the code for F5 if you choose to use this. I have found a mix of values for this (i could not get my test keypress event to pick up the F5 event, hence my note above) so i recommend running the event once with a brake point, inspecting the code, then brake and update your code, then test your logic.
Like the rest i'm not really sure what you want your custom logic to do.
Clairifcation: I'm trying to understand your question, so what you want is: When in Microsoft Picture Manager when you press F5 you want the image that is currently being viewed to be moved to a particular directory? Now if you are writing your own picture viewer and move software then i think we can help if it is the above i'm not really sure you can do that.

Categories