Open a Link on a browser with keyPressed

i want to open a new tab with a link but using the fuction keyPressed
something like; if i press CTRL+A open a tab on the browser with a specif link

1 Like

I suggest using the Java Desktop class. Here’s an example:

import java.awt.Desktop;
import java.net.URI;
import java.io.IOException;
import java.net.URISyntaxException;

Desktop desktop;
boolean controlPressed = false;

void setup() {
  size(200, 200);
  desktop = Desktop.getDesktop();
}

void draw() {
  // must keep window running to register keys
}

void keyPressed() {
  if (keyCode == 17) { // Control
    controlPressed = true;
  }
  if (keyCode == 65 && controlPressed) { // A
    openWebPage("https://discourse.processing.org/");
  }
}

void keyReleased() {
  if (keyCode == 17) { // Control
    controlPressed = false;
  }
}

void openWebPage(String siteUrl) {
  try {
    URI site = new URI(siteUrl);
    if (Desktop.isDesktopSupported() && desktop.isSupported(Desktop.Action.BROWSE)) {
      desktop.browse(site);
    } else {
      println("Desktop does not support browse");
    }
  } catch(URISyntaxException e) {
    e.printStackTrace();
  } catch(IOException e) {
    e.printStackTrace();
  }
}
2 Likes

Links to link() reference:

ProcessingJS.org/reference/link_/

Processing.GitHub.io/processing-javadocs/everything/processing/core/PApplet.html#link-java.lang.String-

2 Likes

thank you so much, this is really helpfull!!! i have to do this like 5 times in my project, do you know a “simpler” way to do this? thanks again :stuck_out_tongue:

2 Likes

You could encapsulate it in a class. Which is just good practice.

BrowserOpener browserOpener;

void setup() {
  size(200, 200);
  browserOpener = new BrowserOpener();
  // A
  browserOpener.addSite(65, "https://discourse.processing.org/");
  // S
  browserOpener.addSite(83, "https://discourse.processing.org/t/open-a-link-on-a-browser-with-keypressed/6740");
}

void draw() {
  // must keep window running to register keys
}

void keyPressed() {
  browserOpener.keyDown(keyCode);
}

void keyReleased() {
  browserOpener.keyUp(keyCode);
}
import java.awt.Desktop;
import java.net.URI;
import java.io.IOException;
import java.net.URISyntaxException;

class BrowserOpener {
  
  ArrayList<SiteKey> siteKeys;
  boolean controlPressed;
  Desktop desktop;
  
  BrowserOpener() {
    siteKeys = new ArrayList<SiteKey>();
    controlPressed = false;
    desktop = Desktop.getDesktop();
  }
  
  void addSite(int codeForKey, String site) {
    siteKeys.add(new SiteKey(codeForKey, site));
  }
  
  void keyDown(int codeForKey) {
    if (codeForKey == 17) {
      controlPressed = true;
    } else if (controlPressed) {
      for (int i = 0; i < siteKeys.size(); i++) {
        if (codeForKey == siteKeys.get(i).codeForKey) {
          openSite(i);
        }
      }
    }
  }
  
  void keyUp(int codeForKey) {
    if (codeForKey == 17) {
      controlPressed = false;
    }
  }
  
  void openSite(int index) {
    try {
      URI site = new URI(siteKeys.get(index).site);
      if (Desktop.isDesktopSupported() && desktop.isSupported(Desktop.Action.BROWSE)) {
        desktop.browse(site);
      } else {
        println("Desktop does not support browse");
      }
    } catch(URISyntaxException e) {
      e.printStackTrace();
    } catch(IOException e) {
      e.printStackTrace();
    }
  }
}

class SiteKey {
  int codeForKey;
  String site;
  SiteKey(int codeForKey, String site) {
    this.codeForKey = codeForKey;
    this.site = site;
  }
}
2 Likes

@figraham, what are important differences between using link() and what you can do with the java.awt.Desktop approach?

1 Like

Please provide a brief description of the links.

Apologizes I thought link() was only implemented within Processing.js because that’s the only place I saw it in documentation. Just saw @GoToLoop’s link to the javadocs. I’d say there’s basically no point to using Desktop over link. Would recommend that @_megss0 replaces the openWebPage() method with the link() method.

I’ve posted link() as an alternative answer to your Desktop-based solution.

Obviously I had to use Pjs’ reference instead b/c Processing’s arbitrarily hides lotsa good API from us!

The fact it was a Pjs link shouldn’t be interpreted as being exclusive to it, since I gave it as a Java Mode answer!

Though I remember link() was once part of Processing’s reference by the time of P1.5.1.

Later on, I remembered to include a Processing JavaDocs reference too.

1 Like

Well, one good argument for the standalone approach is that link is semi-documented / semi-hidden – which I suppose means it is always possible that it could be removed in the future. Certainly, not finding it in the reference can be confusing for beginners – although I’m honestly happy that it isn’t widely used by beginners; it could be frustrating if people were frequently embedding the equivalent of pop-ups in their sketches.

1 Like

That’d be an unfortunate, shortsighted, arbitrary decision then. :clown_face:
This link() subject keeps coming from time to time to our Processing forum: :link:

Its Desktop replacement is a tad too complex for Processing’s target audience. :neutral_face: