win32 icon indicating copy to clipboard operation
win32 copied to clipboard

Monitoring USB ports

Open DarkPurpleKnight opened this issue 4 years ago • 10 comments

Hi Tim,

For a Flutter app for the Windows Desktop, I'm looking for a way to monitor changes to the USB ports. FYI, the Flutter app should respond to plugging in or unplugging a USB joystick/gamepad.

Does the win32 package provide a suitable feature that I have overlooked?

DarkPurpleKnight avatar Jun 12 '21 20:06 DarkPurpleKnight

Looks like RegisterDeviceNotification is the way to go about it: https://docs.microsoft.com/en-us/windows/win32/devio/registering-for-device-notification

However, this requires intercepting the message loop for a WM_DEVICECHANGE message. I think the only way you can do that is from the runner itself; I don't think there's any extensibility mechanism built into Flutter to allow this to be intercepted from Dart, even with the APIs wrapped with win32.

@cbracken, any thoughts on this?

timsneath avatar Jun 16 '21 05:06 timsneath

Oh -- wondering if SetWindowsHookEx might allow installation of a hookproc to monitor these messages. Will investigate...

timsneath avatar Jun 22 '21 18:06 timsneath

Flutter 2.10 includes stable Windows support.

@timsneath Does this have a positive effect on monitoring USB ports on Windows?

DarkPurpleKnight avatar Feb 07 '22 22:02 DarkPurpleKnight

Not particularly: the same notes above apply, since this is really about integrating with the operating system through calling Win32 APIs rather than anything in the UI itself.

One minor update is that we now have an experimental runner written in Dart: https://pub.dev/packages/win32_runner

This is not supported or maintained by the team as a whole, and it has some sizeable caveats (e.g. it supports packages but not plugins) but it provides another path to writing the message handler above.

timsneath avatar Feb 07 '22 22:02 timsneath

@timsneath I didn't know the Dart runner yet. Thanks for your work and the tip.

DarkPurpleKnight avatar Feb 08 '22 14:02 DarkPurpleKnight

Oh -- wondering if SetWindowsHookEx might allow installation of a hookproc to monitor these messages. Will investigate...

@timsneath Can we use this function to hook message in flutter? I have tried this with WH_KEYBOARD_LL but it doesn't work

yanshouwang avatar Mar 01 '22 07:03 yanshouwang

Would https://api.flutter.dev/flutter/dart-ffi/NativeCallable/NativeCallable.listener.html help with any of this?

@yanshouwang here you mention something about creating a message-only window in dart. Can you share more details about that?

mortenboye avatar Oct 30 '23 13:10 mortenboye

Would https://api.flutter.dev/flutter/dart-ffi/NativeCallable/NativeCallable.listener.html help with any of this?

@yanshouwang here you mention something about creating a message-only window in dart. Can you share more details about that?

Here is the code I use to listen drives state, win32_util

yanshouwang avatar Oct 31 '23 02:10 yanshouwang

Would https://api.flutter.dev/flutter/dart-ffi/NativeCallable/NativeCallable.listener.html help with any of this? @yanshouwang here you mention something about creating a message-only window in dart. Can you share more details about that?

Here is the code I use to listen drives state, win32_util

Thank you very much! I'll take a look.

mortenboye avatar Nov 01 '23 00:11 mortenboye

What's the latest update on this ?

SPiercer avatar Mar 03 '24 22:03 SPiercer