Recent

Author Topic: [ANSWERED] Detect if the Fn key is pressed  (Read 5602 times)

CM630

  • Hero Member
  • *****
  • Posts: 1550
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
[ANSWERED] Detect if the Fn key is pressed
« on: June 17, 2020, 10:56:32 pm »
Is it possible to detect if the Fn key, available in some keyboards is pressed?
Here is what is present in classesh.inc:


Code: Pascal  [Select][+][-]
  1. { Types used by standard events }
  2.   TShiftStateEnum = (ssShift, ssAlt, ssCtrl,
  3.     ssLeft, ssRight, ssMiddle, ssDouble,
  4.     // Extra additions
  5.     ssMeta, ssSuper, ssHyper, ssAltGr, ssCaps, ssNum,
  6.     ssScroll,ssTriple,ssQuad,ssExtra1,ssExtra2);
There is no Fn in it.


I just saw, that Fn + something generates VK_Something. It seems to solve the case.
« Last Edit: June 18, 2020, 01:10:41 pm by CM630 »
Лазар 4,4 32 bit (sometimes 64 bit); FPC3,2,2

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Detect if the Fn key is pressed
« Reply #1 on: June 17, 2020, 11:41:21 pm »
Hi!

Put an Edit component on a form an press an Fn key:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState );
  2.     var msg ,s: String ;
  3.     sh : TShiftStateEnum;
  4. begin
  5. msg := '';
  6. for sh in  Shift do
  7.   begin
  8.     writeStr(s,sh);
  9.     msg := msg + s +' / ';
  10.   end;
  11. msg := msg + LineEnding+IntToStr(key);
  12. showMessage(msg);
  13. end;                                      

Winni

rvk

  • Hero Member
  • *****
  • Posts: 6905
Re: Detect if the Fn key is pressed
« Reply #2 on: June 17, 2020, 11:50:35 pm »
The Fn usually doesn't have a scancode.
https://stackoverflow.com/a/2778020

If it does return one, it might not on other systems.
The Fn changes the scancode of other keys.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Detect if the Fn key is pressed
« Reply #3 on: June 17, 2020, 11:54:56 pm »
The Fn usually doesn't have a scancode.
https://stackoverflow.com/a/2778020

If it does return one, it might not on other systems.
The Fn changes the scancode of other keys.

That is also visible with my code sniplet.

Winni

rvk

  • Hero Member
  • *****
  • Posts: 6905
Re: Detect if the Fn key is pressed
« Reply #4 on: June 17, 2020, 11:57:44 pm »
That is also visible with my code sniplet.
Apparently, according to that topic, it could give a non-standard scancode, depending on the system/keyboard. I just wanted to clarify that because you can't rely on it if it gives you one.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8515
Re: Detect if the Fn key is pressed
« Reply #5 on: June 18, 2020, 09:00:23 am »
Apparently, according to that topic, it could give a non-standard scancode, depending on the system/keyboard. I just wanted to clarify that because you can't rely on it if it gives you one.

OP hasn't told us the operating system(s) in which he is interested.

My experience is that that sort of thing is likely to vary by widget set, and to potentially be messed up if going through any sort of remote control connection.

On a PC, it might- or might not- be intercepted by either hardware or by System Management Mode code and used solely to generate a "real" scancode at BIOS level, i.e. way below any application or even operating system involvement.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

rvk

  • Hero Member
  • *****
  • Posts: 6905
Re: [Solved] Detect if the Fn key is pressed
« Reply #6 on: June 18, 2020, 09:18:04 am »
I just saw, that Fn + something generates VK_Something. It seems to solve the case.
Of course does pressing Fn + function key generate VK_functionkey. That's how it works.
But you didn't detect if it was pressed with an Fn key. You just detect if the function key (F1..F12) is pressed.

If that was all you where after, you didn't need to ask about the Fn key because that doesn't matter.

On some keyboards you need to press Fn in combination with F1..F12 to reach those function keys. Otherwise you get other functions. For the ultimate scancodes which you receive from the keyboard, it doesn't contain information if it was in combo with Fn.

BTW. If you don't like the Fn key, some systems let you disable it in the BIOS so that pressing the F1..F12 will directly activate the key as function key.(without the Fn).

PascalDragon

  • Hero Member
  • *****
  • Posts: 6235
  • Compiler Developer
Re: Detect if the Fn key is pressed
« Reply #7 on: June 18, 2020, 09:22:03 am »
On a PC, it might- or might not- be intercepted by either hardware or by System Management Mode code and used solely to generate a "real" scancode at BIOS level, i.e. way below any application or even operating system involvement.

It's likely not at the BIOS level, but at the level of the keyboard itself. E.g. my tablet has a keyboard that can work both directly connected (USB through pogo pins) or through bluetooth and it uses a function key as well. In short: don't expect to be able to determine the state of the Fn key.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8515
Re: Detect if the Fn key is pressed
« Reply #8 on: June 18, 2020, 09:38:52 am »
On a PC, it might- or might not- be intercepted by either hardware or by System Management Mode code and used solely to generate a "real" scancode at BIOS level, i.e. way below any application or even operating system involvement.

It's likely not at the BIOS level, but at the level of the keyboard itself. E.g. my tablet has a keyboard that can work both directly connected (USB through pogo pins) or through bluetooth and it uses a function key as well. In short: don't expect to be able to determine the state of the Fn key.

Remember that the BIOS is very much in play even with modern OSes, since it's there- in conjunction with SMM- that power on/off, volume control etc. buttons are handled.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

CM630

  • Hero Member
  • *****
  • Posts: 1550
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: [Solved] Detect if the Fn key is pressed
« Reply #9 on: June 18, 2020, 09:46:53 am »

I just saw, that Fn + something generates VK_Something. It seems to solve the case.

Of course does pressing Fn + function key generate VK_functionkey. That's how it works.
But you didn't detect if it was pressed with an Fn key. You just detect if the function key (F1..F12) is pressed.
Ooops, yes.

Here is the values of key that I get:

  Fn+F1 = 18
F1 = 112

  Fn+F8 = 178
  F8 = 112

  Fn+D = 68
  D = 68

It seems that the keyboard generates some extra KEY values only for combiantion of Fn+Fx. In all other cases the Fn is ignored.
But oddly, when I tried to associate Fn to a shortcut on the desktop of Win10, when I press Fn+C Windows captured it as ctrl+alt+C. But when I press Fn+C the shortcut is not launched. It gets launched when I press ctrl+alt+C.

So it means that Windows detects somehow that Fn is pressed.

In Lazarus procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); gets triggered when I press Alt or Ctrl (without another key), but it is not triggered when I press Fn only.

There is nothing in the BIOS for that key, it is a standalone keyboard attached to a desktop PC.
« Last Edit: June 18, 2020, 09:53:13 am by CM630 »
Лазар 4,4 32 bit (sometimes 64 bit); FPC3,2,2

PascalDragon

  • Hero Member
  • *****
  • Posts: 6235
  • Compiler Developer
Re: [Solved] Detect if the Fn key is pressed
« Reply #10 on: June 18, 2020, 09:55:27 am »
But oddly, when I tried to associate Fn to a shortcut on the desktop of Win10, when I press Fn+C Windows captured it as ctrl+alt+C. But when I press Fn+C the shortcut is not launched. It gets launched when I press ctrl+alt+C.

That is because shortcuts must use Ctrl+Alt. Windows only received the C and added the Ctrl+Alt itself. ;)

rvk

  • Hero Member
  • *****
  • Posts: 6905
Re: [Solved] Detect if the Fn key is pressed
« Reply #11 on: June 18, 2020, 09:57:35 am »
So it means that Windows detects somehow that Fn is pressed.
No, it means the keyboard just sends a different scancode to the computer when that Fn is pressed in combo. The Fn itself is not send.

The computer doesn't even know it's dealing with a keyboard which has a Fn key.

Scancode 178 is KEY_MEDIA_STOP
Scancode 119 is F8
See https://www.appgamekit.com/documentation/guides/scancodes.htm

The computer doesn't know these are on the same key. On another keyboard they could be separate keys. So it's just the keyboard which decides what to send to the computer.
« Last Edit: June 18, 2020, 10:02:00 am by rvk »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Detect if the Fn key is pressed
« Reply #12 on: June 18, 2020, 10:06:45 am »
Hi!

To be more exact: It is up to the keyboard driver.
If you know how to write a keyboard driver you could change the behaviour.

A good description is here:

https://stackoverflow.com/questions/24423724/finding-the-scan-code-of-fn-key

Winni

rvk

  • Hero Member
  • *****
  • Posts: 6905
Re: Detect if the Fn key is pressed
« Reply #13 on: June 18, 2020, 10:13:47 am »
To be more exact: It is up to the keyboard driver.
If you know how to write a keyboard driver you could change the behaviour.
That would depend on the keyboard.
If the keyboard doesn't send out a scancode for Fn alone, everything is done inside the keyboard.

Otherwise my function keys won't work on my old DOS machine, which they do (without driver).

MarkMLl

  • Hero Member
  • *****
  • Posts: 8515
Re: Detect if the Fn key is pressed
« Reply #14 on: June 18, 2020, 10:37:03 am »
Hold on: Are we talking about the function keys F1  through F12, which have scancodes even if they don't have (ASCII) character codes, or the special Fn key on laptops. etc that is used to enable function and special keys when they're shared with ordinary ones?

I was assuming the latter, i.e. the key literally marked "Fn", which wasn't even common during the DOS era.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018