track_it_windows 0.0.1 copy "track_it_windows: ^0.0.1" to clipboard
track_it_windows: ^0.0.1 copied to clipboard

PlatformWindows

Provides windows support for the track_it plugin.

example/lib/main.dart

import 'dart:developer';
import 'dart:io';

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:path/path.dart' as path;



import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
// import 'package:path_provider/path_provider.dart';
import 'package:track_it_windows/track_it_windows.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final String platformVersion = 'Unknown';
  final _trackItWindowsPlugin = TrackItWindows();
  bool isMouseHovering = false;
  List<Uint8List> imageList = [];
  Map<String, int> events = {};
  Timer? timer;
  

  @override
  void initState() {
    super.initState();
  }

  String getUniqueString(){
    DateTime now = DateTime.now();
  
  // Extract individual components
    String year = now.year.toString();
    String month = now.month.toString().padLeft(2, '0'); // pad with zero if necessary
    String day = now.day.toString().padLeft(2, '0');
    String hour = now.hour.toString().padLeft(2, '0');
    String minute = now.minute.toString().padLeft(2, '0');
    String second = now.second.toString().padLeft(2, '0');
    String ms = now.millisecond.toString();

    // Combine components without spaces and hyphens
    String formattedDateTime = year + month + day + hour + minute + second + ms;

    return formattedDateTime;
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  // Future<void> initPlatformState() async {
  //   String platformVersion;
  //   // Platform messages may fail, so we use a try/catch PlatformException.
  //   // We also handle the message potentially returning null.
  //   try {
  //     platformVersion =
  //         await _trackItWindowsPlugin.getPlatformVersion() ?? 'Unknown platform version';
  //   } on PlatformException {
  //     platformVersion = 'Failed to get platform version.';
  //   }

  //   // If the widget was removed from the tree while the asynchronous platform
  //   // message was in flight, we want to discard the reply rather than calling
  //   // setState to update our non-existent appearance.
  //   if (!mounted) return;

  //   setState(() {
  //     _platformVersion = platformVersion;
  //   });
  // }

  Future<void> getScreenShot() async {
    Directory directory = await getApplicationDocumentsDirectory();
    String directoryPath =directory.path;
    Directory(directoryPath).createSync(recursive: true); // Create directory if it doesn't exist
    String dateTime = getUniqueString();
    String filePath = path.join(directoryPath, 'screenshots', '$dateTime.png');

    // String filePath = '$directoryPath\screenshots\$dateTime.png';
    log(filePath);

    try {
      File imgFile = File(filePath);
      Uint8List? img = await _trackItWindowsPlugin.getScreenCapture();
      log(img!.length.toString());
        await imgFile.writeAsBytes(img);
        log('Image saved successfully.');
        } catch (e) {
        log('Error saving image: $e');
    }

  }

  Future<void> getEvents() async{
    events = await _trackItWindowsPlugin.getEvents();
    debugPrint("$events");
    setState(() {
      
    }); 
  }

  Future<void> startEventMonitor() async {
    await _trackItWindowsPlugin.startEventMonitor();
    timer?.cancel();
    timer = Timer.periodic(const Duration(seconds:  2), (t) async {
      await getEvents();
    });
  }

  Future<void> stopEventMonitor() async{
    final data = await _trackItWindowsPlugin.stopEventMonitor();
    debugPrint("$data");
  }

  // getMouseLeftClickCount() async {
  //   final val = await _trackItWindowsPlugin.getMouseLeftClickCount();
  //   log(val.toString());
  // }

  // getMouseRightClickCount()async{
  //   final val = await _trackItWindowsPlugin.getMouseRightClickCount();
  //   log(val.toString());
  // }

  getMouseTotalClickCount() async {
    final val = await _trackItWindowsPlugin.getMouseTotalClickCount();
    log(val.toString());
  }

  // getLocalKeyCount()async{
  //   final val = await _trackItWindowsPlugin.getLocalKeyCount();
  //   log(val.toString());
  // }

  updateIsMouseHovering(bool val){
    setState(() {
      isMouseHovering = val;
    });
  }

  getGlobalKeyCount()async{

      String prev = "";
    while(isMouseHovering){
      final val = await _trackItWindowsPlugin.getMousePosition();
      if(prev != val.toString()){
        log(val.toString());
        prev = val.toString();
      }
      await Future.delayed(const Duration(milliseconds: 10));
    }
  }

  getTotalKeyCount()async {
    final val = await _trackItWindowsPlugin.getTotalKeyCount();
    log(val.toString());
  }

  Future<void> getListOfImages() async {
    debugPrint("ImageList tap called");

    final data = await _trackItWindowsPlugin.getAllPhysicalScreenshots();

    debugPrint("Data length: ${data.length}");

    if (mounted) {
      setState(() {
        imageList = data;
      });
    }
  }


  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title:  Text('Running on: $platformVersion\n'),
        ),
        // body: Text("$events"),
        // body: ListView.builder(
        //   itemCount: imageList.length,
        //   itemBuilder: (context, index) {
        //     return Card(
        //       margin: const EdgeInsets.all(8.0),
        //       child: Image.memory(
        //         imageList[index],
        //         fit: BoxFit.cover,
        //         errorBuilder: (context, error, stackTrace) => const Icon(Icons.broken_image),
        //       ),
        //     );
        //   },
        // ),
        // floatingActionButton: IconButton(icon: const Icon(Icons.abc),onPressed: ()async{
        //   // await getListOfImages();
        //   await getEvents();
        // }),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              InkWell(
                onTap: (){
                  getScreenShot();
                },
                child:const Text("Take Screen shot")
              ),
              // InkWell(
              //   onTap: (){
              //     getMouseLeftClickCount();
              //   },
              //   child:const Text("Left Mouse Click count")
              // ),
              // InkWell(
              //   onTap: (){
              //     getMouseRightClickCount();
              //   },
              //   child:const Text("Right Mouse Click count")
              // ),
              // InkWell(
              //   onTap: (){
              //     getMouseTotalClickCount();
              //   },
              //   child:const Text("Total Mouse Click count")
              // ),
              // InkWell(
              //   onTap: (){
              //     updateIsMouseHovering(true);
              //     getGlobalKeyCount();
              //   },
              //   child:const Text("Activate Mouse Hovering")
              // ),
              InkWell(
                onTap: () async {
                  await startEventMonitor();
                },
                child:const Text("start events")
              ),
              InkWell(
                onTap: () async {
                  await stopEventMonitor();
                },
                child:const Text("stop events ")
              ),
            ],
          ),
        ),
      ),
    );
  }
}
0
likes
130
points
26
downloads

Publisher

unverified uploader

Weekly Downloads

Provides windows support for the track_it plugin.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on track_it_windows

Packages that implement track_it_windows