如何在 Electron 应用程序外复制和粘贴?

How to copy and paste outside an Electron app?

如果我的问题已经得到解答,请提前致歉,至于我,我在搜索时没有找到任何答案。我目前正在构建一个 Angular 桌面应用程序,使用 Electron 和 electron-packager。我需要能够将应用程序内部的文本复制并粘贴到另一个 window,并且以其他方式相同。如果我留在应用程序中,我已经可以复制和粘贴了。 我已经尝试了社区建议的许多解决方案,正如您将在我的代码中看到的那样,通过向我的应用程序添加一个菜单以及复制和粘贴快捷方式。

下面是我启动电子的代码 window (main.js) :

const { app, BrowserWindow, Menu} = require("electron");
const path = require("path");
const url = require("url");

let win;

function createWindow() {
  win = new BrowserWindow({icon:  path.join(__dirname, `/src/logo4.ico`)});
  win.maximize();

  // Create the Application's main menu
  const template = [{
    label: "Application",
    submenu: [
      {label: "About Application", selector: "orderFrontStandardAboutPanel:"},
      {type: "separator"},
      {
        label: "Quit", accelerator: "Command+Q", click: function () {
          app.quit();
        }
      }
    ]
  }, {
    label: "Edit",
    submenu: [
      {label: "Undo", accelerator: "Ctrl+Z", selector: "undo:"},
      {label: "Redo", accelerator: "Shift+Ctrl+Z", selector: "redo:"},
      {type: "separator"},
      {label: "Cut", accelerator: "Ctrl+X", selector: "cut:"},
      {label: "Copy", accelerator: "Ctrl+C", selector: "copy:"},
      {label: "Paste", accelerator: "Ctrl+V", selector: "paste:"},
      {label: "Select All", accelerator: "Ctrl+A", selector: "selectAll:"}
    ]
  }
  ];

  Menu.setApplicationMenu(Menu.buildFromTemplate(template));
  // Menu.setApplicationMenu(null);

  // load the dist folder from Angular
  win.loadURL(
    url.format({
      pathname: path.join(__dirname, `/dist/index.html`),
      protocol: "file:",
      slashes: true
    })
  );

  // The following is optional and will open the DevTools:
  // win.webContents.openDevTools()

  win.on("closed", () => {
    win = null;
  });
}

app.on("ready", createWindow);

// on macOS, closing the window doesn't quit the app
app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});

// initialize the app's main window
app.on("activate", () => {
  if (win === null) {
    createWindow();
  }
});

如您所见,我尝试将菜单和快捷方式添加到应用程序。它可以很好地在应用程序内复制和粘贴,但不能更多,即使我发现的来源表明它也应该在外面工作。希望你能帮助我 :) 我已经确定问题与 angular 或打包程序无关,因为我试图从 electron . 打开的 window 复制并粘贴到一个简单的 html 形式,它仍然无法正常工作。这是下面的表格,以备不时之需:

<div class="login-wrapper" fxLayout="row" fxLayoutAlign="center center">

  <mat-card class="box">
    <mat-card-header>
      <mat-card-title>Log in</mat-card-title>
    </mat-card-header>

    <form class="example-form" [formGroup]="loginForm" (ngSubmit)="onSubmit()">
      <mat-card-content>
        <mat-form-field class="example-full-width" [ngClass]="{'error': loginForm.controls['username'].errors && !loginForm.controls['username'].pristine}" dividerColor="{{loginForm.controls['username'].errors && !loginForm.controls['username'].pristine ? 'warn' : 'primary'}}">
          <input type="text" matInput placeholder="Username" formControlName="username" required>
          <mat-error *ngIf="loginForm.controls['username'].errors && !loginForm.hasError('required')" class="error-msg">
             Username is required !
          </mat-error>
        </mat-form-field>

        <mat-form-field class="example-full-width">
          <input type="password" matInput placeholder="Password" formControlName="password" required>
          <mat-error *ngIf="loginForm.controls['password'].errors && !loginForm.hasError('required')" class="error-msg">
            Password is required
          </mat-error>
        </mat-form-field>
      </mat-card-content>
      <button type="submit"  [disabled]="!loginForm.valid" class="btn-block" color="accent" mat-stroked-button><span *ngIf="loading" class="spinner-border spinner-border-sm mr-1"></span>
        Log in</button>
    </form>
      <mat-checkbox [(ngModel)]="remember" (change)="rememberChange()"> Remember 
         me</mat-checkbox>
      </mat-card>

    </div>

祝你今天愉快,提前感谢社区

我正在使用 Angular 8.2.13、electron 7.1.1 和 electron-packager 13.1.1。我为 Windows 打包我的应用程序。

N.B:我是 Whosebug 上 post 的新手,抱歉,如果我的 post 不在正确的位置

对于那些可能和我有同样问题的人,我已经解决了。事实上,它与代码无关,只是公司的防病毒软件在我每次启动它时都会将我的应用程序隔离。将其列入白名单,然后一切都可用于复制和粘贴。只是很遗憾我没有早点想出来