Electron、Webpack、React,使用剪贴板时;未定义“fs”

5lhxktic  于 2023-06-04  发布在  Electron
关注(0)|答案(1)|浏览(383)

所以我通过export ELECTRON_START_URL=http://localhost:8080 && electron .在react / webpack / electron桌面应用程序中运行electron。我试图从系统剪贴板上读取粘贴组合键(ctrl + v)。
所以电子工程,我没有一个复制和粘贴快捷方式启用在我的菜单定义(我不打算有一个菜单)。
我正在尝试读取react应用程序中一个组件上的按键组合...类似于:

import React, { useState, useCallback, useEffect, useRef } from "react";

import {clipboard} from 'electron';

const Thing = (props) {

  const keyboardHandler = (e) => {
    if (e.keyCode === 86 && e.ctrlKey) {
      let pasted = clipboard.readText();
      console.log(pasted);
    }
  }

  useEffect(() => {
    window.addEventListener('keydown', keyboardHandler);
    // Remove event listeners on cleanup
    return () => {
      window.removeEventListener('keydown', keyboardHandler);
    };
  }, []);
 
  return <SomeComp onKeyDown={ (e) => keyBoardHandler(e)}/>

}

export default Thing;

我得到了错误:

external_"electron":1 Uncaught ReferenceError: require is not defined
    at eval (external_"electron":1)
    at Object.electron (bundle.js:1167)

编辑:这是我的main.js(electron)代码:

const { app, BrowserWindow, clipboard } = require('electron');

const path = require('path');
const url = require('url');
let mainWindow;

function createWindow () {
  const startUrl = process.env.ELECTRON_START_URL || url.format({
    pathname: path.join(__dirname, '../index.html'),
    protocol: 'file:',
    slashes: true,
  });
  mainWindow = new BrowserWindow({ width: 700, height: 210, frame: true, transprent: true, hasShadow: true });
  mainWindow.loadURL(startUrl);
  mainWindow.on('closed', function () {
    mainWindow = null;
  });
}

app.commandLine.appendSwitch("enable-transparent-visuals");
app.on('ready', createWindow);
app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});
app.on('activate', function () {
  if (mainWindow === null) {
    createWindow();
  }
});
y3bcpkx1

y3bcpkx11#

在require节点函数中添加“window.”,如下所示:

window.require('electron');

这对我来说很有效,希望你也是。

相关问题