无法在Javascript中检测到F11按键

时间:2019-02-19 05:32:20

标签: javascript dom-events

keypress keydown keyup -当同时按下 F11 键时,这些事件均不会触发Mac和Windows。这是预期的行为吗?

我创建了一个demo

var logSpace = document.getElementById("log");

var log = function(event) {
  console.log(event.type, event);
  var p = document.createElement("p");
  p.innerText = event.type + ":" + event.code;
  logSpace.appendChild(p);
}

document.body.addEventListener("keydown", log);

1 个答案:

答案 0 :(得分:0)

在此上下文中未定义logSpace元素,但如果没有它,您的代码在Chrome / Windows中就可以正常工作

import datetime
import time
import os
import numpy as np
import tensorflow.keras as keras
import tensorflow.keras.backend as K
import tensorflow as tf
import socket
_hostname = socket.gethostname()
_mydir = os.path.dirname(os.path.realpath(__file__))
tf.enable_eager_execution()

m = 10000
m_test = 1000
n = 5
X = np.random.randn(m, n)
X_test = np.random.randn(m_test, n)
A = np.random.randn(n)
y = X.dot(A) + np.random.randn(m) * 0.1
y_test = X_test.dot(A) + np.random.randn(m_test) * 0.1
epochs = 100
batch_size = 1024
learning_rate = 0.001
version = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tb_output_dir = os.path.join(_mydir, f'/tmp/junk/{_hostname}/{version}')

file_writer = tf.contrib.summary.create_file_writer(logdir=tb_output_dir)
file_writer.set_as_default()

class CustomCallback(keras.callbacks.Callback):
    def on_train_begin(self, logs={}):
        self.losses = []

    def on_batch_end(self, batch, logs={}):
        from g18e.usr.cottrell import crak
        # logs Out[22]: {'batch': 0, 'size': 1024, 'loss': 10.59116}
        crak('asdf')
        self.losses.append(logs.get('loss'))

def create_model(n, learning_rate):
    x_input = keras.layers.Input(shape=(n,))
    x = x_input
    for i, k in enumerate([32, 16, 16]):
        x = keras.layers.Dense(k, name=f'x_{i}', activation='tanh')(x)
    tf.summary.scalar('look_at_me', K.sum(x))
    output = keras.layers.Dense(1, name='x_final')(x)
    model = keras.models.Model(inputs=x_input, outputs=output)
    optimizer = keras.optimizers.Adam(learning_rate=learning_rate)
    loss = keras.losses.MeanSquaredError()
    model.compile(optimizer, loss)
    return model

Q = create_model(n, learning_rate)

tb_callback = tf.keras.callbacks.TensorBoard(log_dir=tb_output_dir,
    histogram_freq=10,
    batch_size=128,
    write_graph=True,
    write_grads=True,
    write_images=True,
    embeddings_freq=0,
    embeddings_layer_names=None,
    embeddings_metadata=None,
    embeddings_data=None,
    update_freq='epoch', # batch epoch or int
    profile_batch=2,
)
custom_callback = CustomCallback() # log_dir=tb_output_dir, batch_size=128, update_freq='epoch')
callbacks = [custom_callback, tb_callback]

l = Q.fit(X, y, batch_size=batch_size, epochs=epochs, verbose=1, callbacks=callbacks, validation_data=[X_test, y_test])