Winforms shortcut key in X#?

This forum is meant for questions and discussions about the X# language and tools
Post Reply
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

Winforms shortcut key in X#?

Post by ic2 »

I have this working in C#in an OnWindowKeyUp event (I think it should also work on a KeyDown event), in WinForms:

Code: Select all

if (e.Key == Key.Down && Keyboard.Modifiers == ModifierKeys.Control)
// Ctrl cursor, do something
This is the code ILSpy produces:

Code: Select all

if (e:Key == Key.Down) .AND. (Keyboard.Modifiers == ModifierKeys.Control)
This gives no less dan 3 compiler errors. One is solved by adding Using Windows.System.Input but the XS0103 (The name 'KeyBoard' does not exist in the current context and XS1061 'System.Windows.Forms.KeyEventArgs' does not contain a definition for 'Key' and no accessible extension method 'Key' accepting a first argument of type 'System.Windows.Forms.KeyEventArgs' could be found (are you missing a using directive or an assembly reference?)

remain. In C# the tooltip says Keyboard also using System.Windows.Input

What should I change to catch a Ctrl/Alt/Shift + key in Winforms X#? And if the syntax from ILSpy is not correct, can anyone explain why not?

Dick
User avatar
Fabrice
Posts: 458
Joined: Thu Oct 08, 2015 7:47 am
Location: France

Winforms shortcut key in X#?

Post by Fabrice »

Hi Dick,
May be my ILSpy-Plugin is wrong, but... the code you mention is not enough to reply.
You are talking about WinForm but Windows.System.Input is for WPF.
This may explain the missing Key definition.
For WinForms, add a Using System.Windows.Forms.
As I don't know exactly what you want to achieve, I suggest you have a look at the MS Documentation : https://learn.microsoft.com/en-us/dotne ... work-4.7.2

Fab
XSharp Development Team
fabrice(at)xsharp.eu
User avatar
Chris
Posts: 4905
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Winforms shortcut key in X#?

Post by Chris »

Hi Dick,

As Fabrice said, this c# code is not WinForms code, it's WPF code.

The equivalent WinForms code would be something like (after making sure you have a USING System.Windows.Forms in the top of your prg):

IF e:KeyCode == Keys.Down .and. Control.ModifierKeys == Keys.Control

Of course you also need to have a reference to the System.Windows.Forms library and remove any WPF references (libraries) may you have added.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

Winforms shortcut key in X#?

Post by ic2 »

Hello Chris, Fabrice,

Ahhh, you are absolutely right. I was indeed looking in a solution where I have both WPF and Winfoms....and picked the WPF sample. Thanks!

Fabrice, from you reply I wondered: do you have to provide "translation" for all language elements? That seems a bit of an impossible task. I thought that the IL would help find X# syntax using the compiler "reversed" , just like the compiler creates valid IL from X#.

For future readers:

If you click in an empty space of the WInforms window, select properties, use the lightning symbol, then go to Key, click on KeyDown, it will create an event called MyWindow_KeyDown.

In the Misc properties, set KeyPreview to true

Here's a sample which reacts on Alt + cursor left and Ctrl F:

Code: Select all

Private Method MyWindow_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) As Void Strict
	If e:KeyCode == Keys.Left .and. Control.ModifierKeys == Keys.Alt
		// Do something
	Endif
	If e:KeyCode == Keys.F .and. Control.ModifierKeys == Keys.Control
		// Do something
	Endif
Return
Dick
User avatar
Fabrice
Posts: 458
Joined: Thu Oct 08, 2015 7:47 am
Location: France

Winforms shortcut key in X#?

Post by Fabrice »

Dick
Fabrice, from you reply I wondered: do you have to provide "translation" for all language elements? That seems a bit of an impossible task. I thought that the IL would help find X# syntax using the compiler "reversed" , just like the compiler creates valid IL from X#.
Mmm...that would be so simple...
When ILSpy is decompile IL, it will call a kind of eventHandler for each structure (if, while, method call, parameters, ... ) which is a leaf in the compiled-tree.
So I have to catch each one, and rewrite it the X# way.
For Example, I'm doing two passes to enumerate all LOCALS, then inject their declaration in the second pass and provide all LOCALS at the beginning of code, etc...
So sometimes, the generated code is not perfect because I just write what is arriving from ILSpy, and looks like C# mostly.
But any sample to have a better translation is welcome ! :)

Fab
XSharp Development Team
fabrice(at)xsharp.eu
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

Winforms shortcut key in X#?

Post by ic2 »

Hello Fabrice,
Fabrice post=24207 userid=312 wrote: So I have to catch each one, and rewrite it the X# way.
But any sample to have a better translation is welcome ! :)
Wow, that's an awful lot of work indeed.

I use it regularly and often it needs a bit of tweaking. Sometimes I don't see why it fails (like with these shortcut keys which obviously was me overlooking WPF). I will keep in mind to report if I needed to change something if this helps you!

Dick
Post Reply