请解释以下代码的工作以及下面提到的一些问题

问题描述

import keyboard
def fun1(r):
    print(r.name)    
keyboard.on_press(fun1)

代码是一个简单的按键记录器,这里到底发生了什么?

到目前为止,我了解的是:

  1. 导入
  2. 功能定义
  3. keyboard.on_press被称为

请解释以下内容

  1. keyboard.on_press(fun1)到底是什么传递给fun1()
  2. 为什么参数对fun1很重要
  3. 如果我不想创建一个函数只是想将我的代码放在[keyboard.on_press("here")]中,那为什么不可能呢?

再问几个问题

with keyboard.Listener(
        on_press=on_press,on_release=on_release) as listener:
    listener.join()
  1. 这里的“ with”语句如何?
  2. .join()的作用{这意味着它没有将其加入主线程}
  3. 我们在哪里写了on_press=on_press {为什么不写一次}

我不知道此查询是否取决于python版本或模块版本。 我正在使用所有最新版本。 到目前为止,我阅读了有关的文档 https://pynput.readthedocs.io/en/latest/keyboard.html 并用谷歌搜索了我所有的问题,但找不到简单的解释。

解决方法

这里有一些带有注释的代码来帮助解释语法:

class DatabaseService {
      ...
      ...
  
  //final CollectionReference bookCollection = Firestore.instance.collection('users');
  //final CollectionReference bookCollection = Firestore.instance.collection('books');
  final CollectionReference globalData = Firestore.instance.collection('global');
      ...
      ... 
Future<String> bookId() async 
    {
        String uniquebookid =  await globalData.document('SomeHardcodedID').updateData(
        {
        'bookcount': FieldValue.increment(1)
        }).then((voidvalue) async 
        {
            String cid = await globalData.getDocuments().then((bookvalue) => bookvalue.documents.single.data['bookcount'].toString());
            return cid;
        });
    return uniquebookid;
    }//future bookId
      ...
      ...
}//class

import keyboard def somefunc(msg): print(msg) # here def fun1(r): # r is keyboard event print(type(r)) # <class 'keyboard._keyboard_event.KeyboardEvent'> print(r.name) # key text somefunc("here") # call some other function keyboard.on_press(fun1) # pass a reference to the other function (on_press will call whatever function we pass),other function must have single parameter while True: pass # keep script running 关键字仅确保即使出现错误也可以正确关闭对象。

With

关于double变量。我知道这看起来很奇怪。该代码使用命名参数。

with keyboard.Listener(
        on_press=on_press,# named parameter is on_press,we are passing a reference to function on_press
        on_release=on_release) as listener:  # named parameter is on_release,we are passing a reference to function on_release
    listener.join()  # wait for listener thread to finish

# Is shortcut syntax for:

listener = keyboard.Listener(on_press=on_press,on_release=on_release)
try:
    ........
finally:
    listener.stop()  # always do this,even if error
listener.join()

关于回调函数,您可以将函数引用传递给另一个函数。

# sample function
def myfunc(x):   # the parameter name is x
    print(x)
    
# We can call myfunc several ways:
myfunc(1)  # pass value directly
myfunc(x=1)  # specify parameter to assign

x=1  # local variable
myfunc(x)  # pass the local variable as parameter
myfunc(x=x)  # specify parameter to assign,pass local variable # this is the double text you're seeing  

我希望这可以使事情变得简单

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...